home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / xmesa2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  85.3 KB  |  2,994 lines

  1. /* $Id: xmesa2.c,v 1.6 1996/10/22 02:46:29 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.0
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: xmesa2.c,v $
  26.  * Revision 1.6  1996/10/22 02:46:29  brianp
  27.  * now use DITHER_SETUP and XDITHER macros
  28.  * use array indexing instead of pointer dereferencing in inner loops
  29.  *
  30.  * Revision 1.5  1996/10/11 00:23:58  brianp
  31.  * fixed dithering bug in write_span_DITHER_pixmap() again!
  32.  *
  33.  * Revision 1.4  1996/10/09 23:09:56  brianp
  34.  * fixed dithering bug in write_span_DITHER_pixmap()
  35.  *
  36.  * Revision 1.3  1996/09/27 01:31:42  brianp
  37.  * removed unused variables
  38.  *
  39.  * Revision 1.2  1996/09/19 03:16:04  brianp
  40.  * new X/Mesa interface with XMesaContext, XMesaVisual, and XMesaBuffer types
  41.  *
  42.  * Revision 1.1  1996/09/13 01:38:16  brianp
  43.  * Initial revision
  44.  *
  45.  */
  46.  
  47.  
  48. /*
  49.  * Mesa/X11 interface, part 2.
  50.  *
  51.  * This file contains the implementations of all the device driver functions.
  52.  */
  53.  
  54.  
  55.  
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <X11/Xlib.h>
  60. #include "GL/xmesa.h"
  61. #include "macros.h"
  62. #include "types.h"
  63. #include "xmesaP.h"
  64.  
  65.  
  66.  
  67. /*
  68.  * Abort with an error message.
  69.  */
  70. static void die( char *s )
  71. {
  72.    fprintf( stderr, "%s\n", s );
  73.    abort();
  74. }
  75.  
  76.  
  77.  
  78. /*
  79.  * The following functions are used to trap XGetImage() calls which
  80.  * generate BadMatch errors if the drawable isn't mapped.
  81.  */
  82.  
  83. static int caught_xgetimage_error = 0;
  84. static int (*old_xerror_handler)( Display *dpy, XErrorEvent *ev );
  85. static unsigned long xgetimage_serial;
  86.  
  87. /*
  88.  * This is the error handler which will be called if XGetImage fails.
  89.  */
  90. static int xgetimage_error_handler( Display *dpy, XErrorEvent *ev )
  91. {
  92.    if (ev->serial==xgetimage_serial && ev->error_code==BadMatch) {
  93.       /* caught the expected error */
  94.       caught_xgetimage_error = 0;
  95.    }
  96.    else {
  97.       /* call the original X error handler, if any.  otherwise ignore */
  98.       if (old_xerror_handler) {
  99.          (*old_xerror_handler)( dpy, ev );
  100.       }
  101.    }
  102.    return 0;
  103. }
  104.  
  105.  
  106. /*
  107.  * Call this right before XGetImage to setup error trap.
  108.  */
  109. static void catch_xgetimage_errors( Display *dpy )
  110. {
  111.    xgetimage_serial = NextRequest( dpy );
  112.    old_xerror_handler = XSetErrorHandler( xgetimage_error_handler );
  113.    caught_xgetimage_error = 0;
  114. }
  115.  
  116.  
  117. /*
  118.  * Call this right after XGetImage to check if an error occured.
  119.  */
  120. static int check_xgetimage_errors( void )
  121. {
  122.    /* restore old handler */
  123.    (void) XSetErrorHandler( old_xerror_handler );
  124.    /* return 0=no error, 1=error caught */
  125.    return caught_xgetimage_error;
  126. }
  127.  
  128.  
  129. /*
  130.  * Read a pixel from an X drawable.
  131.  */
  132. static unsigned long read_pixel( Display *dpy, Drawable d, int x, int y )
  133. {
  134.    XImage *pixel;
  135.    unsigned long p;
  136.    int error;
  137.    catch_xgetimage_errors( dpy );
  138.    pixel = XGetImage( dpy, d, x, y, 1, 1, AllPlanes, ZPixmap );
  139.    error = check_xgetimage_errors();
  140.    if (pixel && !error) {
  141.       p = XGetPixel( pixel, 0, 0 );
  142.    }
  143.    else {
  144.       p = 0;
  145.    }
  146.    if (pixel) {
  147.       XDestroyImage( pixel );
  148.    }
  149.    return p;
  150. }
  151.  
  152.  
  153.  
  154.  
  155. /*
  156.  * Return the size (width,height of the current color buffer.
  157.  * This function should be called by the glViewport function because
  158.  * glViewport is often called when the window gets resized.  We need to
  159.  * update some X/Mesa stuff when that happens.
  160.  * Output:  width - width of buffer in pixels.
  161.  *          height - height of buffer in pixels.
  162.  */
  163. static void get_buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
  164. {
  165.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  166.    Window root;
  167.    int winx, winy;
  168.    unsigned int winwidth, winheight;
  169.    unsigned int bw, d;
  170.  
  171.    XGetGeometry( xmesa->display, xmesa->xm_buffer->frontbuffer, &root,
  172.          &winx, &winy, &winwidth, &winheight, &bw, &d );
  173.  
  174.    *width = winwidth;
  175.    *height = winheight;
  176.  
  177.    if (   winwidth!=xmesa->xm_buffer->width
  178.        || winheight!=xmesa->xm_buffer->height) {
  179.       xmesa->xm_buffer->width = winwidth;
  180.       xmesa->xm_buffer->height = winheight;
  181.       xmesa_alloc_back_buffer( xmesa->xm_buffer );
  182.    }
  183.  
  184.    /* Needed by FLIP macro */
  185.    xmesa->xm_buffer->bottom = (int) winheight - 1;
  186.  
  187.    if (xmesa->xm_buffer->backimage) {
  188.       /* Needed by PIXELADDR1 macro */
  189.       xmesa->xm_buffer->ximage_width1
  190.                   = xmesa->xm_buffer->backimage->bytes_per_line;
  191.       xmesa->xm_buffer->ximage_origin1
  192.                   = (GLubyte *) xmesa->xm_buffer->backimage->data
  193.                     + xmesa->xm_buffer->ximage_width1 * (winheight-1);
  194.  
  195.       /* Needed by PIXELADDR2 macro */
  196.       xmesa->xm_buffer->ximage_width2
  197.                   = xmesa->xm_buffer->backimage->bytes_per_line / 2;
  198.       xmesa->xm_buffer->ximage_origin2
  199.                   = (GLushort *) xmesa->xm_buffer->backimage->data
  200.                     + xmesa->xm_buffer->ximage_width2 * (winheight-1);
  201.  
  202.       /* Needed by PIXELADDR4 macro */
  203.       xmesa->xm_buffer->ximage_width4 = xmesa->xm_buffer->backimage->width;
  204.       xmesa->xm_buffer->ximage_origin4
  205.                   = (GLuint *) xmesa->xm_buffer->backimage->data
  206.                     + xmesa->xm_buffer->ximage_width4 * (winheight-1);
  207.    }
  208. }
  209.  
  210.  
  211. static void finish( GLcontext *ctx )
  212. {
  213.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  214.    if (xmesa) {
  215.       XSync( xmesa->display, False );
  216.    }
  217. }
  218.  
  219.  
  220. static void flush( GLcontext *ctx )
  221. {
  222.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  223.    if (xmesa) {
  224.       XFlush( xmesa->display );
  225.    }
  226. }
  227.  
  228.  
  229.  
  230. static GLboolean set_buffer( GLcontext *ctx, GLenum mode )
  231. {
  232.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  233.    if (mode==GL_FRONT) {
  234.       /* read/write front buffer */
  235.       xmesa->xm_buffer->buffer = xmesa->xm_buffer->frontbuffer;
  236.       xmesa_setup_DD_pointers( ctx );
  237.       return GL_TRUE;
  238.    }
  239.    else if (mode==GL_BACK && xmesa->xm_buffer->db_state) {
  240.       /* read/write back buffer */
  241.       if (xmesa->xm_buffer->backpixmap) {
  242.          xmesa->xm_buffer->buffer = xmesa->xm_buffer->backpixmap;
  243.       }
  244.       else if (xmesa->xm_buffer->backimage) {
  245.          xmesa->xm_buffer->buffer = None;
  246.       }
  247.       else {
  248.          /* just in case, probably a serious error? */
  249.          xmesa->xm_buffer->buffer = xmesa->xm_buffer->frontbuffer;
  250.       }
  251.       xmesa_setup_DD_pointers( ctx );
  252.       return GL_TRUE;
  253.    }
  254.    else {
  255.       return GL_FALSE;
  256.    }
  257. }
  258.  
  259.  
  260.  
  261. static void clear_index( GLcontext *ctx, GLuint index )
  262. {
  263.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  264.    xmesa->clearpixel = (unsigned long) index;
  265.    XSetForeground( xmesa->display, xmesa->xm_buffer->cleargc,
  266.                    (unsigned long) index );
  267. }
  268.  
  269.  
  270. static void clear_color( GLcontext *ctx,
  271.                          GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  272. {
  273.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  274.    xmesa->clearcolor[0] = r;
  275.    xmesa->clearcolor[1] = g;
  276.    xmesa->clearcolor[2] = b;
  277.    xmesa->clearcolor[3] = a;
  278.    xmesa->clearpixel = xmesa_color_to_pixel( xmesa, r, g, b, a );
  279.    XSetForeground( xmesa->display, xmesa->xm_buffer->cleargc,
  280.                    xmesa->clearpixel );
  281. }
  282.  
  283.  
  284. /* Set current color index */
  285. static void set_index( GLcontext *ctx, GLuint index )
  286. {
  287.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  288.    unsigned long p = (unsigned long) index;
  289.    xmesa->pixel = p;
  290.    XSetForeground( xmesa->display, xmesa->xm_buffer->gc1, p );
  291. }
  292.  
  293.  
  294. /* Set current drawing color */
  295. static void set_color( GLcontext *ctx,
  296.                        GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  297. {
  298.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  299.    xmesa->red   = r;
  300.    xmesa->green = g;
  301.    xmesa->blue  = b;
  302.    xmesa->alpha = a;
  303.    xmesa->pixel = xmesa_color_to_pixel( xmesa, r, g, b, a );;
  304.    XSetForeground( xmesa->display, xmesa->xm_buffer->gc1, xmesa->pixel );
  305. }
  306.  
  307.  
  308.  
  309. /* Set index mask ala glIndexMask */
  310. static GLboolean index_mask( GLcontext *ctx, GLuint mask )
  311. {
  312.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  313.    if (xmesa->xm_buffer->buffer==XIMAGE) {
  314.       return GL_FALSE;
  315.    }
  316.    else {
  317.       unsigned long m;
  318.       if (mask==0xffffffff) {
  319.          m = AllPlanes;
  320.       }
  321.       else {
  322.          m = (unsigned long) mask;
  323.       }
  324.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc1, m );
  325.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc2, m );
  326.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->cleargc, m );
  327.       return GL_TRUE;
  328.    }
  329. }
  330.  
  331.  
  332. /* Implements glColorMask() */
  333. static GLboolean color_mask( GLcontext *ctx,
  334.                              GLboolean rmask, GLboolean gmask,
  335.                              GLboolean bmask, GLboolean amask )
  336. {
  337.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  338. #if defined(__cplusplus) || defined(c_plusplus)
  339.    int xclass = xmesa->xm_visual->visinfo->c_class;
  340. #else
  341.    int xclass = xmesa->xm_visual->visinfo->class;
  342. #endif
  343.  
  344.    if (xmesa->xm_buffer->buffer!=XIMAGE
  345.        && (xclass==TrueColor || xclass==DirectColor)) {
  346.       unsigned long m;
  347.       if (rmask && gmask && bmask) {
  348.          m = AllPlanes;
  349.       }
  350.       else {
  351.          m = 0;
  352.          if (rmask)   m |= xmesa->xm_visual->visinfo->red_mask;
  353.          if (gmask)   m |= xmesa->xm_visual->visinfo->green_mask;
  354.          if (bmask)   m |= xmesa->xm_visual->visinfo->blue_mask;
  355.       }
  356.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc1, m );
  357.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc2, m );
  358.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->cleargc, m );
  359.       return GL_TRUE;
  360.    }
  361.    else {
  362.       return GL_FALSE;
  363.    }
  364. }
  365.  
  366.  
  367. /*
  368.  * Set the pixel logic operation.  Return GL_TRUE if the device driver
  369.  * can perform the operation, otherwise return GL_FALSE.  GL_COPY _must_
  370.  * be operational, obviously.
  371.  */
  372. static GLboolean logicop( GLcontext *ctx, GLenum op )
  373. {
  374.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  375.    int func;
  376.    if (!XMesa)  return GL_FALSE;
  377.    if ((xmesa->xm_buffer->buffer==XIMAGE) && op!=GL_COPY) {
  378.       /* X can't do logic ops in Ximages, except for GL_COPY */
  379.       return GL_FALSE;
  380.    }
  381.    switch (op) {
  382.       case GL_CLEAR:        func = GXclear;        break;
  383.       case GL_SET:        func = GXset;        break;
  384.       case GL_COPY:        func = GXcopy;        break;
  385.       case GL_COPY_INVERTED:    func = GXcopyInverted;    break;
  386.       case GL_NOOP:        func = GXnoop;        break;
  387.       case GL_INVERT:        func = GXinvert;    break;
  388.       case GL_AND:        func = GXand;        break;
  389.       case GL_NAND:        func = GXnand;        break;
  390.       case GL_OR:        func = GXor;        break;
  391.       case GL_NOR:        func = GXnor;        break;
  392.       case GL_XOR:        func = GXxor;        break;
  393.       case GL_EQUIV:        func = GXequiv;        break;
  394.       case GL_AND_REVERSE:    func = GXandReverse;    break;
  395.       case GL_AND_INVERTED:    func = GXandInverted;    break;
  396.       case GL_OR_REVERSE:    func = GXorReverse;    break;
  397.       case GL_OR_INVERTED:    func = GXorInverted;    break;
  398.       default:  return GL_FALSE;
  399.    }
  400.    XSetFunction( xmesa->display, xmesa->xm_buffer->gc1, func );
  401.    XSetFunction( xmesa->display, xmesa->xm_buffer->gc2, func );
  402.    return GL_TRUE;
  403. }
  404.  
  405.  
  406. /*
  407.  * Enable/disable dithering
  408.  */
  409. static void dither( GLcontext *ctx, GLboolean enable )
  410. {
  411.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  412.    if (enable) {
  413.       xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
  414.    }
  415.    else {
  416.       xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
  417.    }
  418.    xmesa_setup_DD_pointers( ctx );
  419. }
  420.  
  421.  
  422.  
  423. /**********************************************************************/
  424. /*** glClear implementations                                        ***/
  425. /**********************************************************************/
  426.  
  427. static void clear_pixmap( GLcontext *ctx, GLboolean all,
  428.                           GLint x, GLint y, GLint width, GLint height )
  429. {
  430.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  431.    if (all) {
  432.       XFillRectangle( xmesa->display, xmesa->xm_buffer->buffer,
  433.                       xmesa->xm_buffer->cleargc,
  434.                       0, 0,
  435.                       xmesa->xm_buffer->width+1, xmesa->xm_buffer->height+1 );
  436.    }
  437.    else {
  438.       XFillRectangle( xmesa->display, xmesa->xm_buffer->buffer,
  439.                       xmesa->xm_buffer->cleargc,
  440.                       x, xmesa->xm_buffer->height - y - height,
  441.                       width, height );
  442.    }
  443. }
  444.  
  445.  
  446. static void clear_8bit_ximage( GLcontext *ctx, GLboolean all,
  447.                                GLint x, GLint y, GLint width, GLint height )
  448. {
  449.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  450.    if (all) {
  451.       size_t n = xmesa->xm_buffer->backimage->bytes_per_line
  452.                * xmesa->xm_buffer->backimage->height;
  453.       MEMSET( xmesa->xm_buffer->backimage->data, xmesa->clearpixel, n );
  454.    }
  455.    else {
  456.       GLint i;
  457.       for (i=0;i<height;i++) {
  458.          GLubyte *ptr = PIXELADDR1( x, y+i );
  459.          MEMSET( ptr, xmesa->clearpixel, width );
  460.       }
  461.    }
  462. }
  463.  
  464.  
  465. static void clear_16bit_ximage( GLcontext *ctx, GLboolean all,
  466.                                 GLint x, GLint y, GLint width, GLint height )
  467. {
  468.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  469.    if (all) {
  470.       register GLuint n;
  471.       register GLushort *ptr2 =(GLushort *) xmesa->xm_buffer->backimage->data;
  472.       register GLushort pixel = (GLushort) xmesa->clearpixel;
  473.       if (xmesa->swapbytes) {
  474.          pixel = ((pixel >> 8) & 0x00ff)
  475.                | ((pixel << 8) & 0xff00);
  476.       }
  477.       if ((pixel & 0xff) == (pixel >> 8)) {
  478.          /* low and high bytes are equal so use memset() */
  479.          n = xmesa->xm_buffer->backimage->bytes_per_line
  480.              * xmesa->xm_buffer->height;
  481.          MEMSET( ptr2, pixel & 0xff, n );
  482.       }
  483.       else {
  484.          n = xmesa->xm_buffer->backimage->bytes_per_line / 2
  485.              * xmesa->xm_buffer->height;
  486.      do {
  487.         *ptr2++ = pixel;
  488.         n--;
  489.      } while (n!=0);
  490.       }
  491.    }
  492.    else {
  493.       register int i, j;
  494.       register GLushort pixel = (GLushort) xmesa->clearpixel;
  495.       for (j=0;j<height;j++) {
  496.      register GLushort *ptr2 = PIXELADDR2( x, y+j );
  497.          for (i=0;i<width;i++) {
  498.             *ptr2++ = pixel;
  499.          }
  500.       }
  501.    }
  502. }
  503.  
  504.  
  505. static void clear_32bit_ximage( GLcontext *ctx, GLboolean all,
  506.                                 GLint x, GLint y, GLint width, GLint height )
  507. {
  508.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  509.    if (all) {
  510.       register GLint n = xmesa->xm_buffer->width * xmesa->xm_buffer->height;
  511.       register GLuint *ptr4 = (GLuint *) xmesa->xm_buffer->backimage->data;
  512.       register GLuint pixel = (GLuint) xmesa->clearpixel;
  513.       if (xmesa->swapbytes) {
  514.          pixel = ((pixel >> 24) & 0x000000ff)
  515.                | ((pixel >> 8)  & 0x0000ff00)
  516.                | ((pixel << 8)  & 0x00ff0000)
  517.                | ((pixel << 24) & 0xff000000);
  518.       }
  519.       if (pixel==0) {
  520.          MEMSET( ptr4, pixel, 4*n );
  521.       }
  522.       else {
  523.          do {
  524.             *ptr4++ = pixel;
  525.             n--;
  526.          } while (n!=0);
  527.       }
  528.    }
  529.    else {
  530.       register int i, j;
  531.       register GLuint pixel = (GLuint) xmesa->clearpixel;
  532.       for (j=0;j<height;j++) {
  533.          register GLuint *ptr4 = PIXELADDR4( x, y+j );
  534.          for (i=0;i<width;i++) {
  535.             *ptr4++ = pixel;
  536.          }
  537.       }
  538.    }
  539. }
  540.  
  541.  
  542. static void clear_nbit_ximage( GLcontext *ctx, GLboolean all,
  543.                                GLint x, GLint y, GLint width, GLint height )
  544. {
  545.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  546.    XImage *img = xmesa->xm_buffer->backimage;
  547.    if (all) {
  548.       register int i, j;
  549.       width = xmesa->xm_buffer->width;
  550.       height = xmesa->xm_buffer->height;
  551.       for (j=0;j<height;j++) {
  552.          for (i=0;i<width;i++) {
  553.             XPutPixel( img, i, j, xmesa->clearpixel );
  554.          }
  555.       }
  556.    }
  557.    else {
  558.       /* TODO: optimize this */
  559.       register int i, j;
  560.       y = FLIP(y);
  561.       for (j=0;j<height;j++) {
  562.          for (i=0;i<width;i++) {
  563.             XPutPixel( img, x+i, y-j, xmesa->clearpixel );
  564.          }
  565.       }
  566.    }
  567. }
  568.  
  569.  
  570.  
  571.  
  572. /*
  573.  * The Mesa library needs to be able to draw pixels in a number of ways:
  574.  *   1. RGB vs Color Index
  575.  *   2. as horizontal spans (polygons, images) vs random locations (points,
  576.  *      lines)
  577.  *   3. different color per-pixel or same color for all pixels
  578.  *
  579.  * Furthermore, the X driver needs to support rendering to 3 possible
  580.  * "buffers", usually one, but sometimes two at a time:
  581.  *   1. The front buffer as an X window
  582.  *   2. The back buffer as a Pixmap
  583.  *   3. The back buffer as an XImage
  584.  *
  585.  * Finally, if the back buffer is an XImage, we can avoid using XPutPixel and
  586.  * optimize common cases such as 24-bit and 8-bit modes.
  587.  *
  588.  * By multiplication, there's at least 48 possible combinations of the above.
  589.  *
  590.  * Below are implementations of the most commonly used combinations.  They are
  591.  * accessed through function pointers which get initialized here and are used
  592.  * directly from the Mesa library.  The 8 function pointers directly correspond
  593.  * to the first 3 cases listed above.
  594.  *
  595.  *
  596.  * The function naming convention is:
  597.  *
  598.  *   write_[span|pixels]_[mono]_[format]_[pixmap|ximage]
  599.  *
  600.  * New functions optimized for specific cases can be added without too much
  601.  * trouble.  An example might be the 24-bit TrueColor mode 8A8R8G8B which is
  602.  * found on IBM RS/6000 X servers.
  603.  */
  604.  
  605.  
  606.  
  607.  
  608. /**********************************************************************/
  609. /*** Write COLOR SPAN functions                                     ***/
  610. /**********************************************************************/
  611.  
  612.  
  613. #define COLOR_SPAN_ARGS    GLcontext *ctx,                    \
  614.             GLuint n, GLint x, GLint y,            \
  615.             const GLubyte red[], const GLubyte green[],    \
  616.             const GLubyte blue[], const GLubyte alpha[],    \
  617.             const GLubyte mask[]
  618.  
  619. /* NOTE: if mask==NULL, draw all pixels */
  620.  
  621.  
  622. /*
  623.  * Write a span of PF_TRUECOLOR pixels to a pixmap.
  624.  */
  625. static void write_span_TRUECOLOR_pixmap( COLOR_SPAN_ARGS )
  626. {
  627.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  628.    Display *dpy = xmesa->xm_visual->display;
  629.    Drawable buffer = xmesa->xm_buffer->buffer;
  630.    GC gc = xmesa->xm_buffer->gc2;
  631.    register GLuint i;
  632.    y = FLIP(y);
  633.    if (mask) {
  634.       for (i=0;i<n;i++,x++) {
  635.          if (mask[i]) {
  636.             XSetForeground( dpy, gc, PACK_RGB( red[i], green[i], blue[i] ) );
  637.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  638.          }
  639.       }
  640.    }
  641.    else {
  642.       /* draw all pixels */
  643.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  644.       for (i=0;i<n;i++) {
  645.          XPutPixel( rowimg, i, 0, PACK_RGB( red[i], green[i], blue[i] ) );
  646.       }
  647.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  648.    }
  649. }
  650.  
  651.  
  652. /*
  653.  * Write a span of PF_8A8B8G8R pixels to a pixmap.
  654.  */
  655. static void write_span_8A8B8G8R_pixmap( COLOR_SPAN_ARGS )
  656. {
  657.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  658.    Display *dpy = xmesa->xm_visual->display;
  659.    Drawable buffer = xmesa->xm_buffer->buffer;
  660.    GC gc = xmesa->xm_buffer->gc2;
  661.    register GLuint i;
  662.    y = FLIP( y );
  663.    if (mask) {
  664.       for (i=0;i<n;i++,x++) {
  665.          if (mask[i]) {
  666.             XSetForeground( dpy, gc,
  667.                          PACK_8A8B8G8R(red[i], green[i], blue[i], alpha[i]) );
  668.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  669.          }
  670.       }
  671.    }
  672.    else {
  673.       /* draw all pixels */
  674.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  675.       register GLuint *ptr4 = (GLuint *) rowimg->data;
  676.       for (i=0;i<n;i++) {
  677.          *ptr4++ = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  678.       }
  679.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  680.    }
  681. }
  682.  
  683.  
  684. /*
  685.  * Write a span of PF_8R8G8B pixels to a pixmap.
  686.  */
  687. static void write_span_8R8G8B_pixmap( COLOR_SPAN_ARGS )
  688. {
  689.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  690.    Display *dpy = xmesa->xm_visual->display;
  691.    Drawable buffer = xmesa->xm_buffer->buffer;
  692.    GC gc = xmesa->xm_buffer->gc2;
  693.    register GLuint i;
  694.    y = FLIP( y );
  695.    if (mask) {
  696.       for (i=0;i<n;i++,x++) {
  697.          if (mask[i]) {
  698.             XSetForeground( dpy, gc, PACK_8R8G8B( red[i], green[i], blue[i] ));
  699.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  700.          }
  701.       }
  702.    }
  703.    else {
  704.       /* draw all pixels */
  705.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  706.       register GLuint *ptr4 = (GLuint *) rowimg->data;
  707.       for (i=0;i<n;i++) {
  708.          *ptr4++ = PACK_8R8G8B( red[i], green[i], blue[i] );
  709.       }
  710.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  711.    }
  712. }
  713.  
  714.  
  715. /*
  716.  * Write a span of PF_5R6G5B pixels to a pixmap.
  717.  */
  718. static void write_span_5R6G5B_pixmap( COLOR_SPAN_ARGS )
  719. {
  720.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  721.    Display *dpy = xmesa->xm_visual->display;
  722.    Drawable buffer = xmesa->xm_buffer->buffer;
  723.    GC gc = xmesa->xm_buffer->gc2;
  724.    register GLuint i;
  725.    y = FLIP( y );
  726.    if (mask) {
  727.       for (i=0;i<n;i++,x++) {
  728.          if (mask[i]) {
  729.             XSetForeground( dpy, gc, PACK_5R6G5B( red[i], green[i], blue[i] ));
  730.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  731.          }
  732.       }
  733.    }
  734.    else {
  735.       /* draw all pixels */
  736.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  737.       register GLushort *ptr2 = (GLushort *) rowimg->data;
  738.       for (i=0;i<n;i++) {
  739.          ptr2[i] = PACK_5R6G5B( red[i], green[i], blue[i] );
  740.       }
  741.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  742.    }
  743. }
  744.  
  745.  
  746. /*
  747.  * Write a span of PF_DITHER pixels to a pixmap.
  748.  */
  749. static void write_span_DITHER_pixmap( COLOR_SPAN_ARGS )
  750. {
  751.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  752.    Display *dpy = xmesa->xm_visual->display;
  753.    Drawable buffer = xmesa->xm_buffer->buffer;
  754.    GC gc = xmesa->xm_buffer->gc2;
  755.    register GLuint i;
  756.    XDITHER_SETUP(y);
  757.    y = FLIP( y );
  758.    if (mask) {
  759.       for (i=0;i<n;i++,x++) {
  760.          if (mask[i]) {
  761.             XSetForeground( dpy, gc, XDITHER(x, red[i], green[i], blue[i]) );
  762.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  763.          }
  764.       }
  765.    }
  766.    else {
  767.       /* draw all pixels */
  768.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  769.       for (i=0;i<n;i++) {
  770.          XPutPixel( rowimg, i, 0, XDITHER(x+i, red[i], green[i], blue[i]) );
  771.       }
  772.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  773.    }
  774. }
  775.  
  776.  
  777. /*
  778.  * Write a span of PF_1BIT pixels to a pixmap.
  779.  */
  780. static void write_span_1BIT_pixmap( COLOR_SPAN_ARGS )
  781. {
  782.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  783.    Display *dpy = xmesa->xm_visual->display;
  784.    Drawable buffer = xmesa->xm_buffer->buffer;
  785.    GC gc = xmesa->xm_buffer->gc2;
  786.    register GLuint i;
  787.    y = FLIP( y );
  788.    if (mask) {
  789.       for (i=0;i<n;i++,x++) {
  790.          if (mask[i]) {
  791.             XSetForeground( dpy, gc,
  792.                             DITHER_1BIT( x, y, red[i], green[i], blue[i] ) );
  793.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  794.          }
  795.       }
  796.    }
  797.    else {
  798.       /* draw all pixels */
  799.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  800.       for (i=0;i<n;i++) {
  801.          XPutPixel( rowimg, i, 0,
  802.                     DITHER_1BIT( x+i, y, red[i], green[i], blue[i] ) );
  803.       }
  804.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  805.    }
  806. }
  807.  
  808.  
  809.  
  810. /*
  811.  * Write a span of PF_HPCR pixels to a pixmap.
  812.  */
  813. static void write_span_HPCR_pixmap( COLOR_SPAN_ARGS )
  814. {
  815.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  816.    Display *dpy = xmesa->xm_visual->display;
  817.    Drawable buffer = xmesa->xm_buffer->buffer;
  818.    GC gc = xmesa->xm_buffer->gc2;
  819.    register GLuint i;
  820.    y = FLIP( y );
  821.    if (mask) {
  822.       for (i=0;i<n;i++,x++) {
  823.          if (mask[i]) {
  824.             XSetForeground( dpy, gc,
  825.                             DITHER_HPCR( x, y, red[i], green[i], blue[i] ) );
  826.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  827.          }
  828.       }
  829.    }
  830.    else {
  831.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  832.       register GLubyte *ptr = (GLubyte *) xmesa->xm_buffer->rowimage->data;
  833.       for (i=0;i<n;i++) {
  834.          ptr[i] = DITHER_HPCR( (x+i), y, red[i], green[i], blue[i] );
  835.       }
  836.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  837.    }
  838. }
  839.  
  840.  
  841. /*
  842.  * Write a span of PF_LOOKUP pixels to a pixmap.
  843.  */
  844. static void write_span_LOOKUP_pixmap( COLOR_SPAN_ARGS )
  845. {
  846.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  847.    Display *dpy = xmesa->xm_visual->display;
  848.    Drawable buffer = xmesa->xm_buffer->buffer;
  849.    GC gc = xmesa->xm_buffer->gc2;
  850.    register GLuint i;
  851.    LOOKUP_SETUP;
  852.    y = FLIP( y );
  853.    if (mask) {
  854.       for (i=0;i<n;i++,x++) {
  855.          if (mask[i]) {
  856.             XSetForeground( dpy, gc, LOOKUP( red[i], green[i], blue[i] ) );
  857.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  858.          }
  859.       }
  860.    }
  861.    else {
  862.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  863.       for (i=0;i<n;i++) {
  864.          XPutPixel( rowimg, i, 0, LOOKUP(red[i],green[i],blue[i]) );
  865.       }
  866.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  867.    }
  868. }
  869.  
  870.  
  871.  
  872. /*
  873.  * Write a span of PF_GRAYSCALE pixels to a pixmap.
  874.  */
  875. static void write_span_GRAYSCALE_pixmap( COLOR_SPAN_ARGS )
  876. {
  877.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  878.    Display *dpy = xmesa->xm_visual->display;
  879.    Drawable buffer = xmesa->xm_buffer->buffer;
  880.    GC gc = xmesa->xm_buffer->gc2;
  881.    register GLuint i;
  882.    y = FLIP( y );
  883.    if (mask) {
  884.       for (i=0;i<n;i++,x++) {
  885.          if (mask[i]) {
  886.             XSetForeground( dpy, gc, GRAY_RGB( red[i], green[i], blue[i] ) );
  887.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  888.          }
  889.       }
  890.    }
  891.    else {
  892.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  893.       for (i=0;i<n;i++) {
  894.          XPutPixel( rowimg, i, 0, GRAY_RGB(red[i],green[i],blue[i]) );
  895.       }
  896.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  897.    }
  898. }
  899.  
  900.  
  901.  
  902. /*
  903.  * Write a span of PF_TRUECOLOR pixels to an XImage.
  904.  */
  905. static void write_span_TRUECOLOR_ximage( COLOR_SPAN_ARGS )
  906. {
  907.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  908.    XImage *img = xmesa->xm_buffer->backimage;
  909.    register GLuint i;
  910.    y = FLIP(y);
  911.    if (mask) {
  912.       for (i=0;i<n;i++,x++) {
  913.          if (mask[i]) {
  914.             XPutPixel( img, x, y, PACK_RGB( red[i], green[i], blue[i] ) );
  915.          }
  916.       }
  917.    }
  918.    else {
  919.       /* draw all pixels */
  920.       for (i=0;i<n;i++,x++) {
  921.          XPutPixel( img, x, y, PACK_RGB( red[i], green[i], blue[i] ) );
  922.       }
  923.    }
  924. }
  925.  
  926.  
  927. /*
  928.  * Write a span of PF_8A8B8G8R-format pixels to an ximage.
  929.  */
  930. static void write_span_8A8B8G8R_ximage( COLOR_SPAN_ARGS )
  931. {
  932.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  933.    register GLuint i;
  934.    register GLuint *ptr = PIXELADDR4( x, y );
  935.    if (mask) {
  936.       for (i=0;i<n;i++) {
  937.          if (mask[i]) {
  938.             ptr[i] = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  939.          }
  940.       }
  941.    }
  942.    else {
  943.       /* draw all pixels */
  944.       for (i=0;i<n;i++) {
  945.          ptr[i] = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  946.       }
  947.    }
  948. }
  949.  
  950.  
  951. /*
  952.  * Write a span of PF_8R8G8B-format pixels to an ximage.
  953.  */
  954. static void write_span_8R8G8B_ximage( COLOR_SPAN_ARGS )
  955. {
  956.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  957.    register GLuint i;
  958.    register GLuint *ptr = PIXELADDR4( x, y );
  959.    if (mask) {
  960.       for (i=0;i<n;i++) {
  961.          if (mask[i]) {
  962.             ptr[i] = PACK_8R8G8B( red[i], green[i], blue[i] );
  963.          }
  964.       }
  965.    }
  966.    else {
  967.       /* draw all pixels */
  968.       for (i=0;i<n;i++) {
  969.          ptr[i] = PACK_8R8G8B( red[i], green[i], blue[i] );
  970.       }
  971.    }
  972. }
  973.  
  974.  
  975. /*
  976.  * Write a span of PF_5R6G5B-format pixels to an ximage.
  977.  */
  978. static void write_span_5R6G5B_ximage( COLOR_SPAN_ARGS )
  979. {
  980.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  981.    register GLuint i;
  982.    register GLushort *ptr = PIXELADDR2( x, y );
  983.    if (mask) {
  984.       for (i=0;i<n;i++) {
  985.          if (mask[i]) {
  986.             ptr[i] = PACK_5R6G5B( red[i], green[i], blue[i] );
  987.          }
  988.       }
  989.    }
  990.    else {
  991.       /* draw all pixels */
  992.       for (i=0;i<n;i++) {
  993.          ptr[i] = PACK_5R6G5B( red[i], green[i], blue[i] );
  994.       }
  995.    }
  996. }
  997.  
  998.  
  999. /*
  1000.  * Write a span of PF_DITHER pixels to an XImage.
  1001.  */
  1002. static void write_span_DITHER_ximage( COLOR_SPAN_ARGS )
  1003. {
  1004.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1005.    XImage *img = xmesa->xm_buffer->backimage;
  1006.    register GLuint i;
  1007.    int yy = FLIP(y);
  1008.    XDITHER_SETUP(yy);
  1009.    if (mask) {
  1010.       for (i=0;i<n;i++,x++) {
  1011.          if (mask[i]) {
  1012.             XPutPixel( img, x, yy, XDITHER( x, red[i], green[i], blue[i] ) );
  1013.          }
  1014.       }
  1015.    }
  1016.    else {
  1017.       /* draw all pixels */
  1018.       for (i=0;i<n;i++,x++) {
  1019.          XPutPixel( img, x, yy, XDITHER( x, red[i], green[i], blue[i] ) );
  1020.       }
  1021.    }
  1022. }
  1023.  
  1024.  
  1025.  
  1026. /*
  1027.  * Write a span of 8-bit PF_DITHER pixels to an XImage.
  1028.  */
  1029. static void write_span_DITHER8_ximage( COLOR_SPAN_ARGS )
  1030. {
  1031.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1032.    register GLuint i;
  1033.    register GLubyte *ptr = PIXELADDR1( x, y );
  1034.    XDITHER_SETUP(y);
  1035.    if (mask) {
  1036.       for (i=0;i<n;i++,x++) {
  1037.          if (mask[i]) {
  1038.             ptr[i] = XDITHER( x, red[i], green[i], blue[i] );
  1039.          }
  1040.       }
  1041.    }
  1042.    else {
  1043.       for (i=0;i<n;i++,x++) {
  1044.          ptr[i] = XDITHER( x, red[i], green[i], blue[i] );
  1045.       }
  1046.    }
  1047. }
  1048.  
  1049.  
  1050.  
  1051. /*
  1052.  * Write a span of PF_1BIT pixels to an XImage.
  1053.  */
  1054. static void write_span_1BIT_ximage( COLOR_SPAN_ARGS )
  1055. {
  1056.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1057.    XImage *img = xmesa->xm_buffer->backimage;
  1058.    register GLuint i;
  1059.    y = FLIP(y);
  1060.    if (mask) {
  1061.       for (i=0;i<n;i++,x++) {
  1062.          if (mask[i]) {
  1063.             XPutPixel(img, x, y, DITHER_1BIT(x, y, red[i], green[i], blue[i]));
  1064.          }
  1065.       }
  1066.    }
  1067.    else {
  1068.       for (i=0;i<n;i++,x++) {
  1069.          XPutPixel( img, x, y, DITHER_1BIT(x, y, red[i], green[i], blue[i]) );
  1070.       }
  1071.    }
  1072. }
  1073.  
  1074.  
  1075. /*
  1076.  * Write a span of PF_HPCR pixels to an XImage.
  1077.  */
  1078. static void write_span_HPCR_ximage( COLOR_SPAN_ARGS )
  1079. {
  1080.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1081.    register GLuint i;
  1082.    register GLubyte *ptr = PIXELADDR1( x, y );
  1083.    if (mask) {
  1084.       for (i=0;i<n;i++,x++) {
  1085.          if (mask[i]) {
  1086.             ptr[i] = DITHER_HPCR( x, y, red[i], green[i], blue[i] );
  1087.          }
  1088.       }
  1089.    }
  1090.    else {
  1091.       /* draw all pixels */
  1092.       for (i=0;i<n;i++,x++) {
  1093.          ptr[i] = DITHER_HPCR( x, y, red[i], green[i], blue[i] );
  1094.       }
  1095.    }
  1096. }
  1097.  
  1098.  
  1099. /*
  1100.  * Write a span of PF_LOOKUP pixels to an XImage.
  1101.  */
  1102. static void write_span_LOOKUP_ximage( COLOR_SPAN_ARGS )
  1103. {
  1104.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1105.    XImage *img = xmesa->xm_buffer->backimage;
  1106.    register GLuint i;
  1107.    LOOKUP_SETUP;
  1108.    y = FLIP(y);
  1109.    if (mask) {
  1110.       for (i=0;i<n;i++,x++) {
  1111.          if (mask[i]) {
  1112.             XPutPixel( img, x, y, LOOKUP( red[i], green[i], blue[i] ) );
  1113.          }
  1114.       }
  1115.    }
  1116.    else {
  1117.       /* draw all pixels */
  1118.       for (i=0;i<n;i++,x++) {
  1119.          XPutPixel( img, x, y, LOOKUP( red[i], green[i], blue[i] ) );
  1120.       }
  1121.    }
  1122. }
  1123.  
  1124.  
  1125. /*
  1126.  * Write a span of 8-bit PF_LOOKUP pixels to an XImage.
  1127.  */
  1128. static void write_span_LOOKUP8_ximage( COLOR_SPAN_ARGS )
  1129. {
  1130.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1131.    register GLuint i;
  1132.    register GLubyte *ptr = PIXELADDR1( x, y );
  1133.    LOOKUP_SETUP;
  1134.    if (mask) {
  1135.       for (i=0;i<n;i++,x++) {
  1136.          if (mask[i]) {
  1137.             ptr[i] = LOOKUP( red[i], green[i], blue[i] );
  1138.          }
  1139.       }
  1140.    }
  1141.    else {
  1142.       /* draw all pixels */
  1143.       for (i=0;i<n;i++,x++) {
  1144.          ptr[i] = LOOKUP( red[i], green[i], blue[i] );
  1145.       }
  1146.    }
  1147. }
  1148.  
  1149.  
  1150.  
  1151.  
  1152. /*
  1153.  * Write a span of PF_GRAYSCALE pixels to an XImage.
  1154.  */
  1155. static void write_span_GRAYSCALE_ximage( COLOR_SPAN_ARGS )
  1156. {
  1157.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1158.    XImage *img = xmesa->xm_buffer->backimage;
  1159.    register GLuint i;
  1160.    y = FLIP(y);
  1161.    if (mask) {
  1162.       for (i=0;i<n;i++,x++) {
  1163.          if (mask[i]) {
  1164.             XPutPixel( img, x, y, GRAY_RGB( red[i], green[i], blue[i] ) );
  1165.          }
  1166.       }
  1167.    }
  1168.    else {
  1169.       /* draw all pixels */
  1170.       for (i=0;i<n;i++,x++) {
  1171.          XPutPixel( img, x, y, GRAY_RGB( red[i], green[i], blue[i] ) );
  1172.       }
  1173.    }
  1174. }
  1175.  
  1176.  
  1177. /*
  1178.  * Write a span of 8-bit PF_GRAYSCALE pixels to an XImage.
  1179.  */
  1180. static void write_span_GRAYSCALE8_ximage( COLOR_SPAN_ARGS )
  1181. {
  1182.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1183.    register GLuint i;
  1184.    register GLubyte *ptr = PIXELADDR1( x, y );
  1185.    if (mask) {
  1186.       for (i=0;i<n;i++) {
  1187.          if (mask[i]) {
  1188.             ptr[i] = GRAY_RGB( red[i], green[i], blue[i] );
  1189.          }
  1190.       }
  1191.    }
  1192.    else {
  1193.       /* draw all pixels */
  1194.       for (i=0;i<n;i++) {
  1195.          ptr[i] = GRAY_RGB( red[i], green[i], blue[i] );
  1196.       }
  1197.    }
  1198. }
  1199.  
  1200.  
  1201.  
  1202.  
  1203. /**********************************************************************/
  1204. /*** Write COLOR PIXEL functions                                    ***/
  1205. /**********************************************************************/
  1206.  
  1207.  
  1208. #define COLOR_PIXEL_ARGS   GLcontext *ctx,                \
  1209.                GLuint n, const GLint x[], const GLint y[],    \
  1210.                const GLubyte red[], const GLubyte green[],    \
  1211.                const GLubyte blue[], const GLubyte alpha[],    \
  1212.                const GLubyte mask[]
  1213.  
  1214.  
  1215. /*
  1216.  * Write an array of PF_TRUECOLOR pixels to a pixmap.
  1217.  */
  1218. static void write_pixels_TRUECOLOR_pixmap( COLOR_PIXEL_ARGS )
  1219. {
  1220.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1221.    Display *dpy = xmesa->xm_visual->display;
  1222.    Drawable buffer = xmesa->xm_buffer->buffer;
  1223.    GC gc = xmesa->xm_buffer->gc2;
  1224.    register GLuint i;
  1225.    for (i=0;i<n;i++) {
  1226.       if (mask[i]) {
  1227.      XSetForeground( dpy, gc, PACK_RGB( red[i], green[i], blue[i] ) );
  1228.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1229.       }
  1230.    }
  1231. }
  1232.  
  1233.  
  1234. /*
  1235.  * Write an array of PF_8A8B8G8R pixels to a pixmap.
  1236.  */
  1237. static void write_pixels_8A8B8G8R_pixmap( COLOR_PIXEL_ARGS )
  1238. {
  1239.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1240.    Display *dpy = xmesa->xm_visual->display;
  1241.    Drawable buffer = xmesa->xm_buffer->buffer;
  1242.    GC gc = xmesa->xm_buffer->gc2;
  1243.    register GLuint i;
  1244.    for (i=0;i<n;i++) {
  1245.       if (mask[i]) {
  1246.      XSetForeground( dpy, gc,
  1247.                          PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] ));
  1248.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1249.       }
  1250.    }
  1251. }
  1252.  
  1253.  
  1254. /*
  1255.  * Write an array of PF_8R8G8B pixels to a pixmap.
  1256.  */
  1257. static void write_pixels_8R8G8B_pixmap( COLOR_PIXEL_ARGS )
  1258. {
  1259.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1260.    Display *dpy = xmesa->xm_visual->display;
  1261.    Drawable buffer = xmesa->xm_buffer->buffer;
  1262.    GC gc = xmesa->xm_buffer->gc2;
  1263.    register GLuint i;
  1264.    for (i=0;i<n;i++) {
  1265.       if (mask[i]) {
  1266.      XSetForeground( dpy, gc, PACK_8R8G8B( red[i], green[i], blue[i] ) );
  1267.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1268.       }
  1269.    }
  1270. }
  1271.  
  1272.  
  1273. /*
  1274.  * Write an array of PF_5R6G5B pixels to a pixmap.
  1275.  */
  1276. static void write_pixels_5R6G5B_pixmap( COLOR_PIXEL_ARGS )
  1277. {
  1278.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1279.    Display *dpy = xmesa->xm_visual->display;
  1280.    Drawable buffer = xmesa->xm_buffer->buffer;
  1281.    GC gc = xmesa->xm_buffer->gc2;
  1282.    register GLuint i;
  1283.    for (i=0;i<n;i++) {
  1284.       if (mask[i]) {
  1285.      XSetForeground( dpy, gc, PACK_5R6G5B( red[i], green[i], blue[i] ) );
  1286.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1287.       }
  1288.    }
  1289. }
  1290.  
  1291.  
  1292. /*
  1293.  * Write an array of PF_DITHER pixels to a pixmap.
  1294.  */
  1295. static void write_pixels_DITHER_pixmap( COLOR_PIXEL_ARGS )
  1296. {
  1297.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1298.    Display *dpy = xmesa->xm_visual->display;
  1299.    Drawable buffer = xmesa->xm_buffer->buffer;
  1300.    GC gc = xmesa->xm_buffer->gc2;
  1301.    register GLuint i;
  1302.    DITHER_SETUP;
  1303.    for (i=0;i<n;i++) {
  1304.       if (mask[i]) {
  1305.      XSetForeground( dpy, gc,
  1306.                          DITHER(x[i], y[i], red[i], green[i], blue[i]) );
  1307.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1308.       }
  1309.    }
  1310. }
  1311.  
  1312.  
  1313. /*
  1314.  * Write an array of PF_1BIT pixels to a pixmap.
  1315.  */
  1316. static void write_pixels_1BIT_pixmap( COLOR_PIXEL_ARGS )
  1317. {
  1318.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1319.    Display *dpy = xmesa->xm_visual->display;
  1320.    Drawable buffer = xmesa->xm_buffer->buffer;
  1321.    GC gc = xmesa->xm_buffer->gc2;
  1322.    register GLuint i;
  1323.    for (i=0;i<n;i++) {
  1324.       if (mask[i]) {
  1325.      XSetForeground( dpy, gc,
  1326.                          DITHER_1BIT( x[i], y[i], red[i], green[i], blue[i] ));
  1327.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1328.       }
  1329.    }
  1330. }
  1331.  
  1332.  
  1333. /*
  1334.  * Write an array of PF_HPCR pixels to a pixmap.
  1335.  */
  1336. static void write_pixels_HPCR_pixmap( COLOR_PIXEL_ARGS )
  1337. {
  1338.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1339.    Display *dpy = xmesa->xm_visual->display;
  1340.    Drawable buffer = xmesa->xm_buffer->buffer;
  1341.    GC gc = xmesa->xm_buffer->gc2;
  1342.    register GLuint i;
  1343.    for (i=0;i<n;i++) {
  1344.       if (mask[i]) {
  1345.          XSetForeground( dpy, gc,
  1346.                          DITHER_HPCR( x[i], y[i], red[i], green[i], blue[i] ));
  1347.          XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1348.       }
  1349.    }
  1350. }
  1351.  
  1352.  
  1353. /*
  1354.  * Write an array of PF_LOOKUP pixels to a pixmap.
  1355.  */
  1356. static void write_pixels_LOOKUP_pixmap( COLOR_PIXEL_ARGS )
  1357. {
  1358.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1359.    Display *dpy = xmesa->xm_visual->display;
  1360.    Drawable buffer = xmesa->xm_buffer->buffer;
  1361.    GC gc = xmesa->xm_buffer->gc2;
  1362.    register GLuint i;
  1363.    LOOKUP_SETUP;
  1364.    for (i=0;i<n;i++) {
  1365.       if (mask[i]) {
  1366.          XSetForeground( dpy, gc, LOOKUP( red[i], green[i], blue[i] ) );
  1367.          XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1368.       }
  1369.    }
  1370. }
  1371.  
  1372.  
  1373. /*
  1374.  * Write an array of PF_GRAYSCALE pixels to a pixmap.
  1375.  */
  1376. static void write_pixels_GRAYSCALE_pixmap( COLOR_PIXEL_ARGS )
  1377. {
  1378.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1379.    Display *dpy = xmesa->xm_visual->display;
  1380.    Drawable buffer = xmesa->xm_buffer->buffer;
  1381.    GC gc = xmesa->xm_buffer->gc2;
  1382.    register GLuint i;
  1383.    for (i=0;i<n;i++) {
  1384.       if (mask[i]) {
  1385.          XSetForeground( dpy, gc, GRAY_RGB( red[i], green[i], blue[i] ) );
  1386.          XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1387.       }
  1388.    }
  1389. }
  1390.  
  1391.  
  1392. /*
  1393.  * Write an array of PF_TRUECOLOR pixels to an ximage.
  1394.  */
  1395. static void write_pixels_TRUECOLOR_ximage( COLOR_PIXEL_ARGS )
  1396. {
  1397.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1398.    XImage *img = xmesa->xm_buffer->backimage;
  1399.    register GLuint i;
  1400.    for (i=0;i<n;i++) {
  1401.       if (mask[i]) {
  1402.      XPutPixel( img, x[i], FLIP(y[i]),
  1403.                     PACK_RGB( red[i], green[i], blue[i] ) );
  1404.       }
  1405.    }
  1406. }
  1407.  
  1408.  
  1409. /*
  1410.  * Write an array of PF_8A8B8G8R pixels to an ximage.
  1411.  */
  1412. static void write_pixels_8A8B8G8R_ximage( COLOR_PIXEL_ARGS )
  1413. {
  1414.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1415.    register GLuint i;
  1416.    for (i=0;i<n;i++) {
  1417.       if (mask[i]) {
  1418.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  1419.          *ptr = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  1420.       }
  1421.    }
  1422. }
  1423.  
  1424.  
  1425. /*
  1426.  * Write an array of PF_8R8G8B pixels to an ximage.
  1427.  */
  1428. static void write_pixels_8R8G8B_ximage( COLOR_PIXEL_ARGS )
  1429. {
  1430.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1431.    register GLuint i;
  1432.    for (i=0;i<n;i++) {
  1433.       if (mask[i]) {
  1434.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  1435.          *ptr = PACK_8R8G8B( red[i], green[i], blue[i] );
  1436.       }
  1437.    }
  1438. }
  1439.  
  1440.  
  1441. /*
  1442.  * Write an array of PF_5R6G5B pixels to an ximage.
  1443.  */
  1444. static void write_pixels_5R6G5B_ximage( COLOR_PIXEL_ARGS )
  1445. {
  1446.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1447.    register GLuint i;
  1448.    for (i=0;i<n;i++) {
  1449.       if (mask[i]) {
  1450.      GLushort *ptr = PIXELADDR2( x[i], y[i] );
  1451.          *ptr = PACK_5R6G5B( red[i], green[i], blue[i] );
  1452.       }
  1453.    }
  1454. }
  1455.  
  1456.  
  1457. /*
  1458.  * Write an array of PF_DITHER pixels to an XImage.
  1459.  */
  1460. static void write_pixels_DITHER_ximage( COLOR_PIXEL_ARGS )
  1461. {
  1462.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1463.    XImage *img = xmesa->xm_buffer->backimage;
  1464.    register GLuint i;
  1465.    DITHER_SETUP;
  1466.    for (i=0;i<n;i++) {
  1467.       if (mask[i]) {
  1468.      XPutPixel( img, x[i], FLIP(y[i]),
  1469.                     DITHER( x[i], y[i], red[i], green[i], blue[i] ) );
  1470.       }
  1471.    }
  1472. }
  1473.  
  1474.  
  1475. /*
  1476.  * Write an array of 8-bit PF_DITHER pixels to an XImage.
  1477.  */
  1478. static void write_pixels_DITHER8_ximage( COLOR_PIXEL_ARGS )
  1479. {
  1480.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1481.    register GLuint i;
  1482.    DITHER_SETUP;
  1483.    for (i=0;i<n;i++) {
  1484.       if (mask[i]) {
  1485.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  1486.      *ptr = DITHER( x[i], y[i], red[i], green[i], blue[i] );
  1487.       }
  1488.    }
  1489. }
  1490.  
  1491.  
  1492. /*
  1493.  * Write an array of PF_1BIT pixels to an XImage.
  1494.  */
  1495. static void write_pixels_1BIT_ximage( COLOR_PIXEL_ARGS )
  1496. {
  1497.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1498.    XImage *img = xmesa->xm_buffer->backimage;
  1499.    register GLuint i;
  1500.    for (i=0;i<n;i++) {
  1501.       if (mask[i]) {
  1502.      XPutPixel( img, x[i], FLIP(y[i]),
  1503.                     DITHER_1BIT( x[i], y[i], red[i], green[i], blue[i] ));
  1504.       }
  1505.    }
  1506. }
  1507.  
  1508.  
  1509. /*
  1510.  * Write an array of PF_HPCR pixels to an XImage.
  1511.  */
  1512. static void write_pixels_HPCR_ximage( COLOR_PIXEL_ARGS )
  1513. {
  1514.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1515.    register GLuint i;
  1516.    for (i=0;i<n;i++) {
  1517.       if (mask[i]) {
  1518.          GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  1519.          *ptr = DITHER_HPCR( x[i], y[i], red[i], green[i], blue[i] );
  1520.       }
  1521.    }
  1522. }
  1523.  
  1524.  
  1525. /*
  1526.  * Write an array of PF_LOOKUP pixels to an XImage.
  1527.  */
  1528. static void write_pixels_LOOKUP_ximage( COLOR_PIXEL_ARGS )
  1529. {
  1530.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1531.    XImage *img = xmesa->xm_buffer->backimage;
  1532.    register GLuint i;
  1533.    LOOKUP_SETUP;
  1534.    for (i=0;i<n;i++) {
  1535.       if (mask[i]) {
  1536.      XPutPixel( img, x[i], FLIP(y[i]), LOOKUP(red[i], green[i], blue[i]) );
  1537.       }
  1538.    }
  1539. }
  1540.  
  1541.  
  1542. /*
  1543.  * Write an array of 8-bit PF_LOOKUP pixels to an XImage.
  1544.  */
  1545. static void write_pixels_LOOKUP8_ximage( COLOR_PIXEL_ARGS )
  1546. {
  1547.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1548.    register GLuint i;
  1549.    LOOKUP_SETUP;
  1550.    for (i=0;i<n;i++) {
  1551.       if (mask[i]) {
  1552.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  1553.      *ptr = LOOKUP( red[i], green[i], blue[i] );
  1554.       }
  1555.    }
  1556. }
  1557.  
  1558.  
  1559. /*
  1560.  * Write an array of PF_GRAYSCALE pixels to an XImage.
  1561.  */
  1562. static void write_pixels_GRAYSCALE_ximage( COLOR_PIXEL_ARGS )
  1563. {
  1564.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1565.    XImage *img = xmesa->xm_buffer->backimage;
  1566.    register GLuint i;
  1567.    for (i=0;i<n;i++) {
  1568.       if (mask[i]) {
  1569.      XPutPixel( img, x[i], FLIP(y[i]),
  1570.                     GRAY_RGB( red[i], green[i], blue[i] ) );
  1571.       }
  1572.    }
  1573. }
  1574.  
  1575.  
  1576. /*
  1577.  * Write an array of 8-bit PF_GRAYSCALE pixels to an XImage.
  1578.  */
  1579. static void write_pixels_GRAYSCALE8_ximage( COLOR_PIXEL_ARGS )
  1580. {
  1581.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1582.    register GLuint i;
  1583.    for (i=0;i<n;i++) {
  1584.       if (mask[i]) {
  1585.      GLubyte *ptr = PIXELADDR1( x[i], y[i] );
  1586.      *ptr = GRAY_RGB( red[i], green[i], blue[i] );
  1587.       }
  1588.    }
  1589. }
  1590.  
  1591.  
  1592.  
  1593.  
  1594. /**********************************************************************/
  1595. /*** Write MONO COLOR SPAN functions                                ***/
  1596. /**********************************************************************/
  1597.  
  1598. #define MONO_SPAN_ARGS    GLcontext *ctx,    \
  1599.              GLuint n, GLint x, GLint y, const GLubyte mask[]
  1600.  
  1601.  
  1602. /*
  1603.  * Write a span of identical pixels to a pixmap.  The pixel value is
  1604.  * the one set by DD.color() or DD.index().
  1605.  */
  1606. static void write_span_mono_pixmap( MONO_SPAN_ARGS )
  1607. {
  1608.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1609.    Display *dpy = xmesa->xm_visual->display;
  1610.    Drawable buffer = xmesa->xm_buffer->buffer;
  1611.    GC gc = xmesa->xm_buffer->gc1;
  1612.    register GLuint i;
  1613.    register GLboolean write_all;
  1614.    y = FLIP( y );
  1615.    write_all = GL_TRUE;
  1616.    for (i=0;i<n;i++) {
  1617.       if (!mask[i]) {
  1618.      write_all = GL_FALSE;
  1619.      break;
  1620.       }
  1621.    }
  1622.    if (write_all) {
  1623.       XFillRectangle( dpy, buffer, gc, (int) x, (int) y, n, 1 );
  1624.    }
  1625.    else {
  1626.       for (i=0;i<n;i++,x++) {
  1627.      if (mask[i]) {
  1628.         XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  1629.      }
  1630.       }
  1631.    }
  1632. }
  1633.  
  1634.  
  1635. /*
  1636.  * Write a span of PF_DITHER pixels to a pixmap.  The pixel value is
  1637.  * the one set by DD.color() or DD.index().
  1638.  */
  1639. static void write_span_mono_DITHER_pixmap( MONO_SPAN_ARGS )
  1640. {
  1641.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1642.    Display *dpy = xmesa->xm_visual->display;
  1643.    Drawable buffer = xmesa->xm_buffer->buffer;
  1644.    GC gc = xmesa->xm_buffer->gc2;
  1645.    register GLuint i;
  1646.    register GLubyte r, g, b;
  1647.    int yy = FLIP( y );
  1648.    XDITHER_SETUP(yy);
  1649.    r = xmesa->red;
  1650.    g = xmesa->green;
  1651.    b = xmesa->blue;
  1652.    for (i=0;i<n;i++,x++) {
  1653.       if (mask[i]) {
  1654.          XSetForeground( dpy, gc, XDITHER( x, r, g, b ) );
  1655.          XDrawPoint( dpy, buffer, gc, (int) x, (int) yy );
  1656.       }
  1657.    }
  1658. }
  1659.  
  1660.  
  1661. /*
  1662.  * Write a span of PF_1BIT pixels to a pixmap.  The pixel value is
  1663.  * the one set by DD.color() or DD.index().
  1664.  */
  1665. static void write_span_mono_1BIT_pixmap( MONO_SPAN_ARGS )
  1666. {
  1667.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1668.    Display *dpy = xmesa->xm_visual->display;
  1669.    Drawable buffer = xmesa->xm_buffer->buffer;
  1670.    GC gc = xmesa->xm_buffer->gc2;
  1671.    register GLuint i;
  1672.    register GLubyte r, g, b;
  1673.    r = xmesa->red;
  1674.    g = xmesa->green;
  1675.    b = xmesa->blue;
  1676.    y = FLIP( y );
  1677.    for (i=0;i<n;i++,x++) {
  1678.       if (mask[i]) {
  1679.          XSetForeground( dpy, gc, DITHER_1BIT( x, y, r, g, b ) );
  1680.          XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  1681.       }
  1682.    }
  1683. }
  1684.  
  1685.  
  1686. /*
  1687.  * Write a span of identical pixels to an XImage.  The pixel value is
  1688.  * the one set by DD.color() or DD.index().
  1689.  */
  1690. static void write_span_mono_ximage( MONO_SPAN_ARGS )
  1691. {
  1692.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1693.    XImage *img = xmesa->xm_buffer->backimage;
  1694.    register GLuint i;
  1695.    register unsigned long p = xmesa->pixel;
  1696.    y = FLIP( y );
  1697.    for (i=0;i<n;i++,x++) {
  1698.       if (mask[i]) {
  1699.      XPutPixel( img, x, y, p );
  1700.       }
  1701.    }
  1702. }
  1703.  
  1704.  
  1705. /*
  1706.  * Write a span of identical 8A8B8G8R pixels to an XImage.  The pixel
  1707.  * value is the one set by DD.color().
  1708.  */
  1709. static void write_span_mono_8A8B8G8R_ximage( MONO_SPAN_ARGS )
  1710. {
  1711.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1712.    GLuint i, p, *ptr;
  1713.    p = (GLuint) xmesa->pixel;
  1714.    ptr = PIXELADDR4( x, y );
  1715.    for (i=0;i<n;i++) {
  1716.       if (mask[i]) {
  1717.      ptr[i] = p;
  1718.       }
  1719.    }
  1720. }
  1721.  
  1722.  
  1723. /*
  1724.  * Write a span of identical 8R8G8B pixels to an XImage.  The pixel
  1725.  * value is the one set by DD.color().
  1726.  */
  1727. static void write_span_mono_8R8G8B_ximage( MONO_SPAN_ARGS )
  1728. {
  1729.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1730.    GLuint i, p, *ptr;
  1731.    p = (GLuint) xmesa->pixel;
  1732.    ptr = PIXELADDR4( x, y );
  1733.    for (i=0;i<n;i++) {
  1734.       if (mask[i]) {
  1735.      ptr[i] = p;
  1736.       }
  1737.    }
  1738. }
  1739.  
  1740.  
  1741. /*
  1742.  * Write a span of identical DITHER pixels to an XImage.  The pixel
  1743.  * value is the one set by DD.color().
  1744.  */
  1745. static void write_span_mono_DITHER_ximage( MONO_SPAN_ARGS )
  1746. {
  1747.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1748.    XImage *img = xmesa->xm_buffer->backimage;
  1749.    register GLuint i;
  1750.    register GLubyte r, g, b;
  1751.    int yy = FLIP(y);
  1752.    XDITHER_SETUP(yy);
  1753.    r = xmesa->red;
  1754.    g = xmesa->green;
  1755.    b = xmesa->blue;
  1756.    for (i=0;i<n;i++,x++) {
  1757.       if (mask[i]) {
  1758.      XPutPixel( img, x, yy, XDITHER( x, r, g, b ) );
  1759.       }
  1760.    }
  1761. }
  1762.  
  1763.  
  1764. /*
  1765.  * Write a span of identical 8-bit DITHER pixels to an XImage.  The pixel
  1766.  * value is the one set by DD.color().
  1767.  */
  1768. static void write_span_mono_DITHER8_ximage( MONO_SPAN_ARGS )
  1769. {
  1770.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1771.    register GLuint i;
  1772.    register GLubyte *ptr = PIXELADDR1(x,y);
  1773.    register GLubyte r, g, b;
  1774.    XDITHER_SETUP(y);
  1775.    r = xmesa->red;
  1776.    g = xmesa->green;
  1777.    b = xmesa->blue;
  1778.    for (i=0;i<n;i++,x++) {
  1779.       if (mask[i]) {
  1780.      ptr[i] = XDITHER( x, r, g, b );
  1781.       }
  1782.    }
  1783. }
  1784.  
  1785.  
  1786. /*
  1787.  * Write a span of identical 8-bit LOOKUP pixels to an XImage.  The pixel
  1788.  * value is the one set by DD.color().
  1789.  */
  1790. static void write_span_mono_LOOKUP8_ximage( MONO_SPAN_ARGS )
  1791. {
  1792.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1793.    register GLuint i;
  1794.    register GLubyte *ptr = PIXELADDR1(x,y);
  1795.    register GLubyte pixel = xmesa->pixel;
  1796.    for (i=0;i<n;i++) {
  1797.       if (mask[i]) {
  1798.      ptr[i] = pixel;
  1799.       }
  1800.    }
  1801. }
  1802.  
  1803.  
  1804. /*
  1805.  * Write a span of identical PF_1BIT pixels to an XImage.  The pixel
  1806.  * value is the one set by DD.color().
  1807.  */
  1808. static void write_span_mono_1BIT_ximage( MONO_SPAN_ARGS )
  1809. {
  1810.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1811.    XImage *img = xmesa->xm_buffer->backimage;
  1812.    register GLuint i;
  1813.    register GLubyte r, g, b;
  1814.    r = xmesa->red;
  1815.    g = xmesa->green;
  1816.    b = xmesa->blue;
  1817.    y = FLIP(y);
  1818.    for (i=0;i<n;i++,x++) {
  1819.       if (mask[i]) {
  1820.      XPutPixel( img, x, y, DITHER_1BIT( x, y, r, g, b ) );
  1821.       }
  1822.    }
  1823. }
  1824.  
  1825.  
  1826. /*
  1827.  * Write a span of identical HPCR pixels to an XImage.  The pixel
  1828.  * value is the one set by DD.color().
  1829.  */
  1830. static void write_span_mono_HPCR_ximage( MONO_SPAN_ARGS )
  1831. {
  1832.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1833.    register GLuint i;
  1834.    register GLubyte *ptr = PIXELADDR1(x,y);
  1835.    register GLubyte r, g, b;
  1836.    r = xmesa->red;
  1837.    g = xmesa->green;
  1838.    b = xmesa->blue;
  1839.    for (i=0;i<n;i++,x++) {
  1840.       if (mask[i]) {
  1841.          ptr[i] = DITHER_HPCR( x, y, r, g, b );
  1842.       }
  1843.    }
  1844. }
  1845.  
  1846.  
  1847.  
  1848. /*
  1849.  * Write a span of identical 8-bit GRAYSCALE pixels to an XImage.  The pixel
  1850.  * value is the one set by DD.color().
  1851.  */
  1852. static void write_span_mono_GRAYSCALE8_ximage( MONO_SPAN_ARGS )
  1853. {
  1854.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1855.    GLuint i;
  1856.    unsigned long p = xmesa->pixel;
  1857.    GLubyte *ptr = PIXELADDR1(x,y);
  1858.    for (i=0;i<n;i++) {
  1859.       if (mask[i]) {
  1860.      ptr[i] = p;
  1861.       }
  1862.    }
  1863. }
  1864.  
  1865.  
  1866.  
  1867.  
  1868. /**********************************************************************/
  1869. /*** Write MONO COLOR PIXELS functions                              ***/
  1870. /**********************************************************************/
  1871.  
  1872. #define MONO_PIXEL_ARGS    GLcontext *ctx,                    \
  1873.             GLuint n, const GLint x[], const GLint y[],    \
  1874.             const GLubyte mask[]
  1875.  
  1876. /*
  1877.  * Write an array of identical pixels to a pixmap.  The pixel value is
  1878.  * the one set by DD.color() or DD.index.
  1879.  */
  1880. static void write_pixels_mono_pixmap( MONO_PIXEL_ARGS )
  1881. {
  1882.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1883.    Display *dpy = xmesa->xm_visual->display;
  1884.    Drawable buffer = xmesa->xm_buffer->buffer;
  1885.    GC gc = xmesa->xm_buffer->gc1;
  1886.    register GLuint i;
  1887.    for (i=0;i<n;i++) {
  1888.       if (mask[i]) {
  1889.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1890.       }
  1891.    }
  1892. }
  1893.  
  1894.  
  1895. /*
  1896.  * Write an array of PF_DITHER pixels to a pixmap.  The pixel value is
  1897.  * the one set by DD.color() or DD.index.
  1898.  */
  1899. static void write_pixels_mono_DITHER_pixmap( MONO_PIXEL_ARGS )
  1900. {
  1901.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1902.    Display *dpy = xmesa->xm_visual->display;
  1903.    Drawable buffer = xmesa->xm_buffer->buffer;
  1904.    GC gc = xmesa->xm_buffer->gc2;
  1905.    register GLuint i;
  1906.    register GLubyte r, g, b;
  1907.    DITHER_SETUP;
  1908.    r = xmesa->red;
  1909.    g = xmesa->green;
  1910.    b = xmesa->blue;
  1911.    for (i=0;i<n;i++) {
  1912.       if (mask[i]) {
  1913.          XSetForeground( dpy, gc, DITHER( x[i], y[i], r, g, b ) );
  1914.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1915.       }
  1916.    }
  1917. }
  1918.  
  1919.  
  1920. /*
  1921.  * Write an array of PF_1BIT pixels to a pixmap.  The pixel value is
  1922.  * the one set by DD.color() or DD.index.
  1923.  */
  1924. static void write_pixels_mono_1BIT_pixmap( MONO_PIXEL_ARGS )
  1925. {
  1926.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1927.    Display *dpy = xmesa->xm_visual->display;
  1928.    Drawable buffer = xmesa->xm_buffer->buffer;
  1929.    GC gc = xmesa->xm_buffer->gc2;
  1930.    register GLuint i;
  1931.    register GLubyte r, g, b;
  1932.    r = xmesa->red;
  1933.    g = xmesa->green;
  1934.    b = xmesa->blue;
  1935.    for (i=0;i<n;i++) {
  1936.       if (mask[i]) {
  1937.          XSetForeground( dpy, gc, DITHER_1BIT( x[i], y[i], r, g, b ) );
  1938.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1939.       }
  1940.    }
  1941. }
  1942.  
  1943.  
  1944. /*
  1945.  * Write an array of identical pixels to an XImage.  The pixel value is
  1946.  * the one set by DD.color() or DD.index.
  1947.  */
  1948. static void write_pixels_mono_ximage( MONO_PIXEL_ARGS )
  1949. {
  1950.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1951.    XImage *img = xmesa->xm_buffer->backimage;
  1952.    register GLuint i;
  1953.    register unsigned long p = xmesa->pixel;
  1954.    for (i=0;i<n;i++) {
  1955.       if (mask[i]) {
  1956.      XPutPixel( img, x[i], FLIP(y[i]), p );
  1957.       }
  1958.    }
  1959. }
  1960.  
  1961.  
  1962.  
  1963. /*
  1964.  * Write an array of identical 8A8B8G8R pixels to an XImage.  The pixel value
  1965.  * is the one set by DD.color().
  1966.  */
  1967. static void write_pixels_mono_8A8B8G8R_ximage( MONO_PIXEL_ARGS )
  1968. {
  1969.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1970.    register GLuint i;
  1971.    register GLuint p = (GLuint) xmesa->pixel;
  1972.    for (i=0;i<n;i++) {
  1973.       if (mask[i]) {
  1974.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  1975.      *ptr = p;
  1976.       }
  1977.    }
  1978. }
  1979.  
  1980.  
  1981. /*
  1982.  * Write an array of identical 8R8G8B pixels to an XImage.  The pixel value
  1983.  * is the one set by DD.color().
  1984.  */
  1985. static void write_pixels_mono_8R8G8B_ximage( MONO_PIXEL_ARGS )
  1986. {
  1987.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1988.    register GLuint i;
  1989.    register GLuint p = (GLuint) xmesa->pixel;
  1990.    for (i=0;i<n;i++) {
  1991.       if (mask[i]) {
  1992.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  1993.      *ptr = p;
  1994.       }
  1995.    }
  1996. }
  1997.  
  1998.  
  1999. /*
  2000.  * Write an array of identical PF_DITHER pixels to an XImage.  The pixel
  2001.  * value is the one set by DD.color().
  2002.  */
  2003. static void write_pixels_mono_DITHER_ximage( MONO_PIXEL_ARGS )
  2004. {
  2005.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2006.    XImage *img = xmesa->xm_buffer->backimage;
  2007.    register GLuint i;
  2008.    register GLubyte r, g, b;
  2009.    DITHER_SETUP;
  2010.    r = xmesa->red;
  2011.    g = xmesa->green;
  2012.    b = xmesa->blue;
  2013.    for (i=0;i<n;i++) {
  2014.       if (mask[i]) {
  2015.      XPutPixel( img, x[i], FLIP(y[i]), DITHER( x[i], y[i], r, g, b ) );
  2016.       }
  2017.    }
  2018. }
  2019.  
  2020.  
  2021. /*
  2022.  * Write an array of identical 8-bit PF_DITHER pixels to an XImage.  The
  2023.  * pixel value is the one set by DD.color().
  2024.  */
  2025. static void write_pixels_mono_DITHER8_ximage( MONO_PIXEL_ARGS )
  2026. {
  2027.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2028.    register GLuint i;
  2029.    register GLubyte r, g, b;
  2030.    DITHER_SETUP;
  2031.    r = xmesa->red;
  2032.    g = xmesa->green;
  2033.    b = xmesa->blue;
  2034.    for (i=0;i<n;i++) {
  2035.       if (mask[i]) {
  2036.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2037.      *ptr = DITHER( x[i], y[i], r, g, b );
  2038.       }
  2039.    }
  2040. }
  2041.  
  2042.  
  2043. /*
  2044.  * Write an array of identical 8-bit PF_LOOKUP pixels to an XImage.  The
  2045.  * pixel value is the one set by DD.color().
  2046.  */
  2047. static void write_pixels_mono_LOOKUP8_ximage( MONO_PIXEL_ARGS )
  2048. {
  2049.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2050.    register GLuint i;
  2051.    register GLubyte pixel = xmesa->pixel;
  2052.    for (i=0;i<n;i++) {
  2053.       if (mask[i]) {
  2054.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2055.      *ptr = pixel;
  2056.       }
  2057.    }
  2058. }
  2059.  
  2060.  
  2061.  
  2062. /*
  2063.  * Write an array of identical PF_1BIT pixels to an XImage.  The pixel
  2064.  * value is the one set by DD.color().
  2065.  */
  2066. static void write_pixels_mono_1BIT_ximage( MONO_PIXEL_ARGS )
  2067. {
  2068.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2069.    XImage *img = xmesa->xm_buffer->backimage;
  2070.    register GLuint i;
  2071.    register GLubyte r, g, b;
  2072.    r = xmesa->red;
  2073.    g = xmesa->green;
  2074.    b = xmesa->blue;
  2075.    for (i=0;i<n;i++) {
  2076.       if (mask[i]) {
  2077.      XPutPixel( img, x[i], FLIP(y[i]), DITHER_1BIT( x[i], y[i], r, g, b ));
  2078.       }
  2079.    }
  2080. }
  2081.  
  2082.  
  2083. /*
  2084.  * Write an array of identical PF_HPCR pixels to an XImage.  The
  2085.  * pixel value is the one set by DD.color().
  2086.  */
  2087. static void write_pixels_mono_HPCR_ximage( MONO_PIXEL_ARGS )
  2088. {
  2089.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2090.    register GLuint i;
  2091.    register GLubyte r, g, b;
  2092.    r = xmesa->red;
  2093.    g = xmesa->green;
  2094.    b = xmesa->blue;
  2095.    for (i=0;i<n;i++) {
  2096.       if (mask[i]) {
  2097.          GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2098.          *ptr = DITHER_HPCR( x[i], y[i], r, g, b );
  2099.       }
  2100.    }
  2101. }
  2102.  
  2103.  
  2104. /*
  2105.  * Write an array of identical 8-bit PF_GRAYSCALE pixels to an XImage.  The
  2106.  * pixel value is the one set by DD.color().
  2107.  */
  2108. static void write_pixels_mono_GRAYSCALE8_ximage( MONO_PIXEL_ARGS )
  2109. {
  2110.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2111.    register GLuint i;
  2112.    register unsigned long p = xmesa->pixel;
  2113.    for (i=0;i<n;i++) {
  2114.       if (mask[i]) {
  2115.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2116.      *ptr = p;
  2117.       }
  2118.    }
  2119. }
  2120.  
  2121.  
  2122.  
  2123.  
  2124. /**********************************************************************/
  2125. /*** Write INDEX SPAN functions                                     ***/
  2126. /**********************************************************************/
  2127.  
  2128. #define INDEX_SPAN_ARGS    GLcontext *ctx,                    \
  2129.             GLuint n, GLint x, GLint y, const GLuint index[], \
  2130.             const GLubyte mask[]
  2131.  
  2132.  
  2133. /*
  2134.  * Write a span of CI pixels to a Pixmap.
  2135.  */
  2136. static void write_span_index_pixmap( INDEX_SPAN_ARGS )
  2137. {
  2138.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2139.    Display *dpy = xmesa->xm_visual->display;
  2140.    Drawable buffer = xmesa->xm_buffer->buffer;
  2141.    GC gc = xmesa->xm_buffer->gc2;
  2142.    register GLuint i;
  2143.    y = FLIP(y);
  2144.    for (i=0;i<n;i++,x++) {
  2145.       if (mask[i]) {
  2146.      XSetForeground( dpy, gc, (unsigned long) index[i] );
  2147.      XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  2148.       }
  2149.    }
  2150. }
  2151.  
  2152.  
  2153. /*
  2154.  * Write a span of CI pixels to an XImage.
  2155.  */
  2156. static void write_span_index_ximage( INDEX_SPAN_ARGS )
  2157. {
  2158.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2159.    XImage *img = xmesa->xm_buffer->backimage;
  2160.    register GLuint i;
  2161.    y = FLIP(y);
  2162.    for (i=0;i<n;i++,x++) {
  2163.       if (mask[i]) {
  2164.      XPutPixel( img, x, y, (unsigned long) index[i] );
  2165.       }
  2166.    }
  2167. }
  2168.  
  2169.  
  2170.  
  2171. /**********************************************************************/
  2172. /*** Write INDEX PIXELS functions                                   ***/
  2173. /**********************************************************************/
  2174.  
  2175. #define INDEX_PIXELS_ARGS    GLcontext *ctx,                \
  2176.                 GLuint n, const GLint x[], const GLint y[], \
  2177.                 const GLuint index[], const GLubyte mask[]
  2178.  
  2179.  
  2180. /*
  2181.  * Write an array of CI pixels to a Pixmap.
  2182.  */
  2183. static void write_pixels_index_pixmap( INDEX_PIXELS_ARGS )
  2184. {
  2185.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2186.    Display *dpy = xmesa->xm_visual->display;
  2187.    Drawable buffer = xmesa->xm_buffer->buffer;
  2188.    GC gc = xmesa->xm_buffer->gc2;
  2189.    register GLuint i;
  2190.    for (i=0;i<n;i++) {
  2191.       if (mask[i]) {
  2192.      XSetForeground( dpy, gc, (unsigned long) index[i] );
  2193.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  2194.       }
  2195.    }
  2196. }
  2197.  
  2198.  
  2199. /*
  2200.  * Write an array of CI pixels to an XImage.
  2201.  */
  2202. static void write_pixels_index_ximage( INDEX_PIXELS_ARGS )
  2203. {
  2204.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2205.    XImage *img = xmesa->xm_buffer->backimage;
  2206.    register GLuint i;
  2207.    for (i=0;i<n;i++) {
  2208.       if (mask[i]) {
  2209.      XPutPixel( img, x[i], FLIP(y[i]), (unsigned long) index[i] );
  2210.       }
  2211.    }
  2212. }
  2213.  
  2214.  
  2215.  
  2216.  
  2217. /**********************************************************************/
  2218. /*****                      Pixel reading                         *****/
  2219. /**********************************************************************/
  2220.  
  2221.  
  2222.  
  2223. /*
  2224.  * Read a horizontal span of color-index pixels.
  2225.  */
  2226. static void read_index_span( GLcontext *ctx,
  2227.                  GLuint n, GLint x, GLint y, GLuint index[] )
  2228. {
  2229.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2230.    int i;
  2231.  
  2232.    y = FLIP(y);
  2233.  
  2234.    if (xmesa->xm_buffer->buffer) {
  2235.       XImage *span;
  2236.       int error;
  2237.       catch_xgetimage_errors( xmesa->display );
  2238.       span = XGetImage( xmesa->display, xmesa->xm_buffer->buffer,
  2239.                 x, y, n, 1, AllPlanes, ZPixmap );
  2240.       error = check_xgetimage_errors();
  2241.       if (span && !error) {
  2242.      for (i=0;i<n;i++) {
  2243.         index[i] = (GLuint) XGetPixel( span, i, 0 );
  2244.      }
  2245.       }
  2246.       else {
  2247.      /* return 0 pixels */
  2248.      for (i=0;i<n;i++) {
  2249.         index[i] = 0;
  2250.      }
  2251.       }
  2252.       if (span) {
  2253.      XDestroyImage( span );
  2254.       }
  2255.    }
  2256.    else if (xmesa->xm_buffer->backimage) {
  2257.       XImage *img = xmesa->xm_buffer->backimage;
  2258.       for (i=0;i<n;i++,x++) {
  2259.      index[i] = (GLuint) XGetPixel( img, x, y );
  2260.       }
  2261.    }
  2262. }
  2263.  
  2264.  
  2265.  
  2266. /*
  2267.  * Read a horizontal span of color pixels.
  2268.  */
  2269. static void read_color_span( GLcontext *ctx,
  2270.                  GLuint n, GLint x, GLint y,
  2271.                              GLubyte red[], GLubyte green[],
  2272.                              GLubyte blue[], GLubyte alpha[] )
  2273. {
  2274.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2275.    register GLuint i;
  2276.  
  2277.    if (xmesa->xm_buffer->buffer) {
  2278.       XImage *span;
  2279.       int error;
  2280.       catch_xgetimage_errors( xmesa->display );
  2281.       span = XGetImage( xmesa->display, xmesa->xm_buffer->buffer,
  2282.                 x, FLIP(y), n, 1, AllPlanes, ZPixmap );
  2283.       error = check_xgetimage_errors();
  2284.       if (span && !error) {
  2285.      switch (xmesa->pixelformat) {
  2286.         case PF_TRUECOLOR:
  2287.                {
  2288.                   GLint rshift = xmesa->xm_visual->rshift, rmult = xmesa->xm_visual->rmult;
  2289.                   GLint gshift = xmesa->xm_visual->gshift, gmult = xmesa->xm_visual->gmult;
  2290.                   GLint bshift = xmesa->xm_visual->bshift, bmult = xmesa->xm_visual->bmult;
  2291.                   for (i=0;i<n;i++) {
  2292.                      unsigned long p = XGetPixel( span, i, 0 );
  2293.                      red[i]   = (GLubyte) ((p >> rshift) & rmult);
  2294.                      green[i] = (GLubyte) ((p >> gshift) & gmult);
  2295.                      blue[i]  = (GLubyte) ((p >> bshift) & bmult);
  2296.                      alpha[i] = 255;
  2297.                   }
  2298.                }
  2299.            break;
  2300.         case PF_8A8B8G8R:
  2301.                {
  2302.                   GLuint *ptr4 = (GLuint *) span->data;
  2303.                   for (i=0;i<n;i++) {
  2304.                      GLuint p4 = *ptr4++;
  2305.                      red[i]   = (GLubyte) ( p4        & 0xff);
  2306.                      green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2307.                      blue[i]  = (GLubyte) ((p4 >> 16) & 0xff);
  2308.                      alpha[i] = (GLubyte) ((p4 >> 24) & 0xff);
  2309.                   }
  2310.            }
  2311.            break;
  2312.             case PF_8R8G8B:
  2313.                {
  2314.                   GLuint *ptr4 = (GLuint *) span->data;
  2315.                   for (i=0;i<n;i++) {
  2316.                      GLuint p4 = *ptr4++;
  2317.                      red[i]   = (GLubyte) ((p4 >> 16) & 0xff);
  2318.                      green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2319.                      blue[i]  = (GLubyte) ( p4        & 0xff);
  2320.                      alpha[i] = 255;
  2321.                   }
  2322.            }
  2323.            break;
  2324.             case PF_5R6G5B:
  2325.                {
  2326.                   GLushort *ptr2 = (GLushort *) span->data;
  2327.                   for (i=0;i<n;i++) {
  2328.                      GLushort p2 = *ptr2++;
  2329.                      red[i]   = (GLubyte) ((p2 >> 11) & 0x1f);
  2330.                      green[i] = (GLubyte) ((p2 >> 5)  & 0x3f);
  2331.                      blue[i]  = (GLubyte) ( p2        & 0x1f);
  2332.                      alpha[i] = 255;
  2333.                   }
  2334.            }
  2335.                break;
  2336.             case PF_HPCR:
  2337.                {
  2338.                   GLubyte *ptr1 = (GLubyte *) span->data;
  2339.                   for (i=0;i<n;i++) {
  2340.                      GLubyte p = *ptr1++;
  2341.                      red[i]   =  p & 0xE0;
  2342.                      green[i] = (p & 0x1C) << 3;
  2343.                      blue[i]  = (p & 0x03) << 6;
  2344.                      alpha[i] = 255;
  2345.                   }
  2346.                }
  2347.                break;
  2348.         case PF_DITHER:
  2349.         case PF_LOOKUP:
  2350.         case PF_GRAYSCALE:
  2351.                {
  2352.                   GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2353.                   GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2354.                   GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2355.                   if (xmesa->xm_visual->visinfo->depth==8) {
  2356.                      GLubyte *ptr1 = (GLubyte *) span->data;
  2357.                      for (i=0;i<n;i++) {
  2358.                         unsigned long p = *ptr1++;
  2359.                         red[i]   = red_table[p];
  2360.                         green[i] = green_table[p];
  2361.                         blue[i]  = blue_table[p];
  2362.                         alpha[i] = 255;
  2363.                      }
  2364.                   }
  2365.                   else {
  2366.                      for (i=0;i<n;i++) {
  2367.                         unsigned long p = XGetPixel( span, i, 0 );
  2368.                         red[i]   = red_table[p];
  2369.                         green[i] = green_table[p];
  2370.                         blue[i]  = blue_table[p];
  2371.                         alpha[i] = 255;
  2372.                      }
  2373.                   }
  2374.                }
  2375.            break;
  2376.         case PF_1BIT:
  2377.            for (i=0;i<n;i++) {
  2378.           unsigned long p = XGetPixel( span, i, 0 );
  2379.           red[i]   = (GLubyte) (p * 255);
  2380.           green[i] = (GLubyte) (p * 255);
  2381.           blue[i]  = (GLubyte) (p * 255);
  2382.           alpha[i] = 255;
  2383.            }
  2384.            break;
  2385.         default:
  2386.            die("Problem in DD.read_color_span (1)");
  2387.      }
  2388.       }
  2389.       else {
  2390.      /* return black pixels */
  2391.      for (i=0;i<n;i++) {
  2392.         red[i] = green[i] = blue[i] = alpha[i] = 0;
  2393.      }
  2394.       }
  2395.       if (span) {
  2396.      XDestroyImage( span );
  2397.       }
  2398.    }
  2399.    else if (xmesa->xm_buffer->backimage) {
  2400.       switch (xmesa->pixelformat) {
  2401.      case PF_TRUECOLOR:
  2402.             {
  2403.                GLint rshift = xmesa->xm_visual->rshift, rmult = xmesa->xm_visual->rmult;
  2404.                GLint gshift = xmesa->xm_visual->gshift, gmult = xmesa->xm_visual->gmult;
  2405.                GLint bshift = xmesa->xm_visual->bshift, bmult = xmesa->xm_visual->bmult;
  2406.                XImage *img = xmesa->xm_buffer->backimage;
  2407.                y = FLIP(y);
  2408.                for (i=0;i<n;i++,x++) {
  2409.                   unsigned long p = XGetPixel( img, x, y );
  2410.                   red[i]   = (GLubyte) ((p >> rshift) & rmult);
  2411.                   green[i] = (GLubyte) ((p >> gshift) & gmult);
  2412.                   blue[i]  = (GLubyte) ((p >> bshift) & bmult);
  2413.                   alpha[i] = 255;
  2414.                }
  2415.             }
  2416.         break;
  2417.      case PF_8A8B8G8R:
  2418.             {
  2419.                GLuint *ptr4 = PIXELADDR4( x, y );
  2420.                for (i=0;i<n;i++) {
  2421.                   GLuint p4 = *ptr4++;
  2422.                   red[i]   = (GLubyte) ( p4        & 0xff);
  2423.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2424.                   blue[i]  = (GLubyte) ((p4 >> 16) & 0xff);
  2425.                   alpha[i] = (GLint)   ((p4 >> 24) & 0xff);
  2426.                }
  2427.             }
  2428.         break;
  2429.      case PF_8R8G8B:
  2430.             {
  2431.                GLuint *ptr4 = PIXELADDR4( x, y );
  2432.                for (i=0;i<n;i++) {
  2433.                   GLuint p4 = *ptr4++;
  2434.                   red[i]   = (GLubyte) ((p4 >> 16) & 0xff);
  2435.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2436.                   blue[i]  = (GLubyte) ( p4        & 0xff);
  2437.                   alpha[i] = 255;
  2438.                }
  2439.             }
  2440.         break;
  2441.          case PF_5R6G5B:
  2442.             {
  2443.                GLushort *ptr2 = PIXELADDR2( x, y );
  2444.                for (i=0;i<n;i++) {
  2445.                   GLushort p2 = *ptr2++;
  2446.                   red[i]   = (GLubyte) ((p2 >> 11) & 0x1f);
  2447.                   green[i] = (GLubyte) ((p2 >> 5)  & 0x3f);
  2448.                   blue[i]  = (GLubyte) ( p2        & 0x1f);
  2449.                   alpha[i] = 255;
  2450.                }
  2451.             }
  2452.             break;
  2453.          case PF_HPCR:
  2454.             {
  2455.                GLubyte *ptr1 = PIXELADDR1( x, y );
  2456.                for (i=0;i<n;i++) {
  2457.                   GLubyte p = *ptr1++;
  2458.                   red[i]   =  p & 0xE0;
  2459.                   green[i] = (p & 0x1C) << 3;
  2460.                   blue[i]  = (p & 0x03) << 6;
  2461.                   alpha[i] = 255;
  2462.                }
  2463.             }
  2464.             break;
  2465.      case PF_DITHER:
  2466.      case PF_LOOKUP:
  2467.      case PF_GRAYSCALE:
  2468.             {
  2469.                GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2470.                GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2471.                GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2472.                if (xmesa->xm_visual->visinfo->depth==8) {
  2473.                   GLubyte *ptr1 = PIXELADDR1(x,y);
  2474.                   for (i=0;i<n;i++) {
  2475.                      unsigned long p = *ptr1++;
  2476.                      red[i]   = red_table[p];
  2477.                      green[i] = green_table[p];
  2478.                      blue[i]  = blue_table[p];
  2479.                      alpha[i] = 255;
  2480.                   }
  2481.                }
  2482.                else {
  2483.                   XImage *img = xmesa->xm_buffer->backimage;
  2484.                   y = FLIP(y);
  2485.                   for (i=0;i<n;i++,x++) {
  2486.                      unsigned long p = XGetPixel( img, x, y );
  2487.                      red[i]   = red_table[p];
  2488.                      green[i] = green_table[p];
  2489.                      blue[i]  = blue_table[p];
  2490.                      alpha[i] = 255;
  2491.                   }
  2492.                }
  2493.             }
  2494.         break;
  2495.      case PF_1BIT:
  2496.             {
  2497.                XImage *img = xmesa->xm_buffer->backimage;
  2498.                y = FLIP(y);
  2499.                for (i=0;i<n;i++,x++) {
  2500.                   unsigned long p = XGetPixel( img, x, y );
  2501.                   red[i]   = (GLubyte) (p * 255);
  2502.                   green[i] = (GLubyte) (p * 255);
  2503.                   blue[i]  = (GLubyte) (p * 255);
  2504.                   alpha[i] = 255;
  2505.                }
  2506.         }
  2507.         break;
  2508.      default:
  2509.         die("Problem in DD.read_color_span (2)");
  2510.       }
  2511.    }
  2512. }
  2513.  
  2514.  
  2515.  
  2516. /*
  2517.  * Read an array of color index pixels.
  2518.  */
  2519. static void read_index_pixels( GLcontext *ctx,
  2520.                    GLuint n, const GLint x[], const GLint y[],
  2521.                                GLuint indx[], const GLubyte mask[] )
  2522. {
  2523.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2524.    register GLuint i;
  2525.    if (xmesa->xm_buffer->buffer) {
  2526.       for (i=0;i<n;i++) {
  2527.          if (mask[i]) {
  2528.             indx[i] = (GLuint) read_pixel( xmesa->display,
  2529.                                            xmesa->xm_buffer->buffer,
  2530.                                            x[i], FLIP(y[i]) );
  2531.          }
  2532.       }
  2533.    }
  2534.    else if (xmesa->xm_buffer->backimage) {
  2535.       XImage *img = xmesa->xm_buffer->backimage;
  2536.       for (i=0;i<n;i++) {
  2537.          if (mask[i]) {
  2538.             indx[i] = (GLuint) XGetPixel( img, x[i], FLIP(y[i]) );
  2539.          }
  2540.       }
  2541.    }
  2542. }
  2543.  
  2544.  
  2545.  
  2546. static void read_color_pixels( GLcontext *ctx,
  2547.                    GLuint n, const GLint x[], const GLint y[],
  2548.                                GLubyte red[], GLubyte green[],
  2549.                                GLubyte blue[], GLubyte alpha[],
  2550.                                const GLubyte mask[] )
  2551. {
  2552.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2553.    Display *dpy = xmesa->xm_visual->display;
  2554.    Drawable buffer = xmesa->xm_buffer->buffer;
  2555.    register GLuint i;
  2556.  
  2557.    if (xmesa->xm_buffer->buffer) {
  2558.       switch (xmesa->pixelformat) {
  2559.      case PF_TRUECOLOR:
  2560.             {
  2561.                GLint rshift = xmesa->xm_visual->rshift, rmult = xmesa->xm_visual->rmult;
  2562.                GLint gshift = xmesa->xm_visual->gshift, gmult = xmesa->xm_visual->gmult;
  2563.                GLint bshift = xmesa->xm_visual->bshift, bmult = xmesa->xm_visual->bmult;
  2564.                for (i=0;i<n;i++) {
  2565.                   if (mask[i] ) {
  2566.                      unsigned long p = read_pixel( dpy, buffer,
  2567.                                                    x[i], FLIP(y[i]) );
  2568.                      red[i]   = (GLubyte) ((p >> rshift) & rmult);
  2569.                      green[i] = (GLubyte) ((p >> gshift) & gmult);
  2570.                      blue[i]  = (GLubyte) ((p >> bshift) & bmult);
  2571.                      alpha[i] = 255;
  2572.                   }
  2573.                }
  2574.         }
  2575.         break;
  2576.      case PF_8A8B8G8R:
  2577.         for (i=0;i<n;i++) {
  2578.                if (mask[i]) {
  2579.                   unsigned long p = read_pixel( dpy, buffer,
  2580.                                                 x[i], FLIP(y[i]) );
  2581.                   red[i]   = (GLubyte) ( p        & 0xff);
  2582.                   green[i] = (GLubyte) ((p >> 8)  & 0xff);
  2583.                   blue[i]  = (GLubyte) ((p >> 16) & 0xff);
  2584.                   alpha[i] = (GLubyte) ((p >> 24) & 0xff);
  2585.                }
  2586.         }
  2587.         break;
  2588.      case PF_8R8G8B:
  2589.         for (i=0;i<n;i++) {
  2590.                if (mask[i]) {
  2591.                   unsigned long p = read_pixel( dpy, buffer,
  2592.                                                 x[i], FLIP(y[i]) );
  2593.                   red[i]   = (GLubyte) ((p >> 16) & 0xff);
  2594.                   green[i] = (GLubyte) ((p >> 8)  & 0xff);
  2595.                   blue[i]  = (GLubyte) ( p        & 0xff);
  2596.                   alpha[i] = 255;
  2597.                }
  2598.         }
  2599.         break;
  2600.          case PF_5R6G5B:
  2601.             for (i=0;i<n;i++) {
  2602.                if (mask[i]) {
  2603.                   unsigned long p = read_pixel( dpy, buffer,
  2604.                                                 x[i], FLIP(y[i]) );
  2605.                   red[i]   = (GLubyte) ((p >> 11) & 0x1f);
  2606.                   green[i] = (GLubyte) ((p >> 5)  & 0x3f);
  2607.                   blue[i]  = (GLubyte) ( p        & 0x1f);
  2608.                   alpha[i] = 255;
  2609.                }
  2610.             }
  2611.             break;
  2612.          case PF_HPCR:
  2613.             {
  2614.                for (i=0;i<n;i++) {
  2615.                   if (mask[i]) {
  2616.                      unsigned long p = read_pixel( dpy, buffer,
  2617.                                                    x[i], FLIP(y[i]) );
  2618.                      red[i]   =  p & 0xE0;
  2619.                      green[i] = (p & 0x1C) << 3;
  2620.                      blue[i]  = (p & 0x03) << 6;
  2621.                      alpha[i] = 255;
  2622.                   }
  2623.                }
  2624.             }
  2625.             break;
  2626.      case PF_DITHER:
  2627.      case PF_LOOKUP:
  2628.      case PF_GRAYSCALE:
  2629.             {
  2630.                GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2631.                GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2632.                GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2633.                for (i=0;i<n;i++) {
  2634.                   if (mask[i]) {
  2635.                      unsigned long p = read_pixel( dpy, buffer,
  2636.                                                    x[i], FLIP(y[i]) );
  2637.                      red[i]   = red_table[p];
  2638.                      green[i] = green_table[p];
  2639.                      blue[i]  = blue_table[p];
  2640.                      alpha[i] = 255;
  2641.                   }
  2642.                }
  2643.         }
  2644.         break;
  2645.      case PF_1BIT:
  2646.         for (i=0;i<n;i++) {
  2647.                if (mask[i]) {
  2648.                   unsigned long p = read_pixel( dpy, buffer,
  2649.                                                 x[i], FLIP(y[i]) );
  2650.                   red[i]   = (GLubyte) (p * 255);
  2651.                   green[i] = (GLubyte) (p * 255);
  2652.                   blue[i]  = (GLubyte) (p * 255);
  2653.                   alpha[i] = 255;
  2654.                }
  2655.         }
  2656.         break;
  2657.      default:
  2658.         die("Problem in DD.read_color_pixels (1)");
  2659.       }
  2660.    }
  2661.    else if (xmesa->xm_buffer->backimage) {
  2662.       switch (xmesa->pixelformat) {
  2663.      case PF_TRUECOLOR:
  2664.             {
  2665.                GLint rshift = xmesa->xm_visual->rshift, rmult = xmesa->xm_visual->rmult;
  2666.                GLint gshift = xmesa->xm_visual->gshift, gmult = xmesa->xm_visual->gmult;
  2667.                GLint bshift = xmesa->xm_visual->bshift, bmult = xmesa->xm_visual->bmult;
  2668.                XImage *img = xmesa->xm_buffer->backimage;
  2669.                for (i=0;i<n;i++) {
  2670.                   if (mask[i]) {
  2671.                      unsigned long p;
  2672.                      p = XGetPixel( img, x[i], FLIP(y[i]) );
  2673.                      red[i]   = (GLubyte) ((p >> rshift) & rmult);
  2674.                      green[i] = (GLubyte) ((p >> gshift) & gmult);
  2675.                      blue[i]  = (GLubyte) ((p >> bshift) & bmult);
  2676.                      alpha[i] = 255;
  2677.                   }
  2678.                }
  2679.         }
  2680.         break;
  2681.      case PF_8A8B8G8R:
  2682.         for (i=0;i<n;i++) {
  2683.            if (mask[i]) {
  2684.                   GLuint *ptr4 = PIXELADDR4( x[i], y[i] );
  2685.                   GLuint p4 = *ptr4;
  2686.                   red[i]   = (GLubyte) ( p4        & 0xff);
  2687.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2688.                   blue[i]  = (GLubyte) ((p4 >> 16) & 0xff);
  2689.                   alpha[i] = (GLubyte) ((p4 >> 24) & 0xff);
  2690.                }
  2691.         }
  2692.         break;
  2693.      case PF_8R8G8B:
  2694.         for (i=0;i<n;i++) {
  2695.            if (mask[i]) {
  2696.                   GLuint *ptr4 = PIXELADDR4( x[i], y[i] );
  2697.                   GLuint p4 = *ptr4;
  2698.                   red[i]   = (GLubyte) ((p4 >> 16) & 0xff);
  2699.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2700.                   blue[i]  = (GLubyte) ( p4        & 0xff);
  2701.                   alpha[i] = 255;
  2702.                }
  2703.         }
  2704.         break;
  2705.          case PF_5R6G5B:
  2706.             for (i=0;i<n;i++) {
  2707.                if (mask[i]) {
  2708.                   GLushort *ptr2 = PIXELADDR2( x[i], y[i] );
  2709.                   GLushort p2 = *ptr2;
  2710.                   red[i]   = (GLubyte) ((p2 >> 11) & 0x1f);
  2711.                   green[i] = (GLubyte) ((p2 >> 5)  & 0x3f);
  2712.                   blue[i]  = (GLubyte) ( p2        & 0x1f);
  2713.                   alpha[i] = 255;
  2714.                }
  2715.             }
  2716.             break;
  2717.          case PF_HPCR:
  2718.             for (i=0;i<n;i++) {
  2719.                if (mask[i]) {
  2720.                   GLubyte *ptr1 = PIXELADDR1( x[i], y[i] );
  2721.                   GLubyte p = *ptr1;
  2722.                   red[i]   =  p & 0xE0;
  2723.                   green[i] = (p & 0x1C) << 3;
  2724.                   blue[i]  = (p & 0x03) << 6;
  2725.                   alpha[i] = 255;
  2726.                }
  2727.             }
  2728.             break;
  2729.      case PF_DITHER:
  2730.      case PF_LOOKUP:
  2731.      case PF_GRAYSCALE:
  2732.             {
  2733.                GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2734.                GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2735.                GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2736.                XImage *img = xmesa->xm_buffer->backimage;
  2737.                for (i=0;i<n;i++) {
  2738.                   if (mask[i]) {
  2739.                      unsigned long p;
  2740.                      p = XGetPixel( img, x[i], FLIP(y[i]) );
  2741.                      red[i]   = red_table[p];
  2742.                      green[i] = green_table[p];
  2743.                      blue[i]  = blue_table[p];
  2744.                      alpha[i] = 255;
  2745.                   }
  2746.                }
  2747.         }
  2748.         break;
  2749.      case PF_1BIT:
  2750.             {
  2751.                XImage *img = xmesa->xm_buffer->backimage;
  2752.                for (i=0;i<n;i++) {
  2753.                   if (mask[i]) {
  2754.                      unsigned long p;
  2755.                      p = XGetPixel( img, x[i], FLIP(y[i]) );
  2756.                      red[i]   = (GLubyte) (p * 255);
  2757.                      green[i] = (GLubyte) (p * 255);
  2758.                      blue[i]  = (GLubyte) (p * 255);
  2759.                      alpha[i] = 255;
  2760.                   }
  2761.                }
  2762.         }
  2763.         break;
  2764.      default:
  2765.         die("Problem in DD.read_color_pixels (1)");
  2766.       }
  2767.    }
  2768. }
  2769.  
  2770.  
  2771.  
  2772.  
  2773. /*
  2774.  * Initialize all the DD.* function pointers depending on the color
  2775.  * buffer configuration.  This is mainly called by XMesaMakeCurrent.
  2776.  */
  2777. void xmesa_setup_DD_pointers( GLcontext *ctx )
  2778. {
  2779.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2780.    /*
  2781.     * Always the same:
  2782.     */
  2783.    ctx->Driver.UpdateState = xmesa_setup_DD_pointers;
  2784.    ctx->Driver.GetBufferSize = get_buffer_size;
  2785.    ctx->Driver.Flush = flush;
  2786.    ctx->Driver.Finish = finish;
  2787.  
  2788.    ctx->Driver.SetBuffer = set_buffer;
  2789.  
  2790.    ctx->Driver.Index = set_index;
  2791.    ctx->Driver.Color = set_color;
  2792.    ctx->Driver.ClearIndex = clear_index;
  2793.    ctx->Driver.ClearColor = clear_color;
  2794.    ctx->Driver.IndexMask = index_mask;
  2795.    ctx->Driver.ColorMask = color_mask;
  2796.    ctx->Driver.LogicOp = logicop;
  2797.    ctx->Driver.Dither = dither;
  2798.  
  2799.    ctx->Driver.PointsFunc = xmesa_get_points_func( ctx );
  2800.    ctx->Driver.LineFunc = xmesa_get_line_func( ctx );
  2801.    ctx->Driver.TriangleFunc = xmesa_get_triangle_func( ctx );
  2802.  
  2803.    /*
  2804.     * These drawing functions depend on color buffer config:
  2805.     */
  2806.    if (xmesa->xm_buffer->buffer!=XIMAGE) {
  2807.       /* Writing to window or back pixmap */
  2808.       ctx->Driver.Clear = clear_pixmap;
  2809.       switch (xmesa->pixelformat) {
  2810.      case PF_INDEX:
  2811.         ctx->Driver.WriteIndexSpan       = write_span_index_pixmap;
  2812.         ctx->Driver.WriteMonoindexSpan   = write_span_mono_pixmap;
  2813.         ctx->Driver.WriteIndexPixels     = write_pixels_index_pixmap;
  2814.         ctx->Driver.WriteMonoindexPixels = write_pixels_mono_pixmap;
  2815.         break;
  2816.      case PF_TRUECOLOR:
  2817.         ctx->Driver.WriteColorSpan       = write_span_TRUECOLOR_pixmap;
  2818.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  2819.         ctx->Driver.WriteColorPixels     = write_pixels_TRUECOLOR_pixmap;
  2820.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  2821.         break;
  2822.      case PF_8A8B8G8R:
  2823.         ctx->Driver.WriteColorSpan       = write_span_8A8B8G8R_pixmap;
  2824.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  2825.         ctx->Driver.WriteColorPixels     = write_pixels_8A8B8G8R_pixmap;
  2826.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  2827.         break;
  2828.      case PF_8R8G8B:
  2829.         ctx->Driver.WriteColorSpan       = write_span_8R8G8B_pixmap;
  2830.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  2831.         ctx->Driver.WriteColorPixels     = write_pixels_8R8G8B_pixmap;
  2832.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  2833.         break;
  2834.      case PF_5R6G5B:
  2835.         ctx->Driver.WriteColorSpan       = write_span_5R6G5B_pixmap;
  2836.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  2837.         ctx->Driver.WriteColorPixels     = write_pixels_5R6G5B_pixmap;
  2838.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  2839.         break;
  2840.      case PF_DITHER:
  2841.         ctx->Driver.WriteColorSpan       = write_span_DITHER_pixmap;
  2842.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_DITHER_pixmap;
  2843.         ctx->Driver.WriteColorPixels     = write_pixels_DITHER_pixmap;
  2844.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_DITHER_pixmap;
  2845.         break;
  2846.      case PF_1BIT:
  2847.         ctx->Driver.WriteColorSpan       = write_span_1BIT_pixmap;
  2848.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_1BIT_pixmap;
  2849.         ctx->Driver.WriteColorPixels     = write_pixels_1BIT_pixmap;
  2850.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_1BIT_pixmap;
  2851.         break;
  2852.          case PF_HPCR:
  2853.             ctx->Driver.WriteColorSpan       = write_span_HPCR_pixmap;
  2854.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  2855.             ctx->Driver.WriteColorPixels     = write_pixels_HPCR_pixmap;
  2856.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  2857.             break;
  2858.          case PF_LOOKUP:
  2859.             ctx->Driver.WriteColorSpan       = write_span_LOOKUP_pixmap;
  2860.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  2861.             ctx->Driver.WriteColorPixels     = write_pixels_LOOKUP_pixmap;
  2862.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  2863.             break;
  2864.          case PF_GRAYSCALE:
  2865.             ctx->Driver.WriteColorSpan       = write_span_GRAYSCALE_pixmap;
  2866.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  2867.             ctx->Driver.WriteColorPixels     = write_pixels_GRAYSCALE_pixmap;
  2868.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  2869.             break;
  2870.      default:
  2871.         die("Bad pixel format in xmesa_setup_DD_pointers (1)");
  2872.       }
  2873.    }
  2874.    else if (xmesa->xm_buffer->buffer==XIMAGE) {
  2875.       /* Writing to back XImage */
  2876.       if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
  2877.          /* Do this on Crays */
  2878.          ctx->Driver.Clear = clear_nbit_ximage;
  2879.       }
  2880.       else {
  2881.          /* Do this on most machines */
  2882.          switch (xmesa->xm_buffer->backimage->bits_per_pixel) {
  2883.             case 8:
  2884.                ctx->Driver.Clear = clear_8bit_ximage;
  2885.                break;
  2886.             case 16:
  2887.                ctx->Driver.Clear = clear_16bit_ximage;
  2888.                break;
  2889.             case 32:
  2890.                ctx->Driver.Clear = clear_32bit_ximage;
  2891.                break;
  2892.             default:
  2893.                ctx->Driver.Clear = clear_nbit_ximage;
  2894.                break;
  2895.          }
  2896.       }
  2897.       switch (xmesa->pixelformat) {
  2898.      case PF_INDEX:
  2899.         ctx->Driver.WriteIndexSpan       = write_span_index_ximage;
  2900.         ctx->Driver.WriteMonoindexSpan   = write_span_mono_ximage;
  2901.         ctx->Driver.WriteIndexPixels     = write_pixels_index_ximage;
  2902.         ctx->Driver.WriteMonoindexPixels = write_pixels_mono_ximage;
  2903.         break;
  2904.      case PF_TRUECOLOR:
  2905.         /* Generic RGB */
  2906.         ctx->Driver.WriteColorSpan       = write_span_TRUECOLOR_ximage;
  2907.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  2908.         ctx->Driver.WriteColorPixels     = write_pixels_TRUECOLOR_ximage;
  2909.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  2910.         break;
  2911.      case PF_8A8B8G8R:
  2912.         ctx->Driver.WriteColorSpan       = write_span_8A8B8G8R_ximage;
  2913.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_8A8B8G8R_ximage;
  2914.         ctx->Driver.WriteColorPixels     = write_pixels_8A8B8G8R_ximage;
  2915.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_8A8B8G8R_ximage;
  2916.         break;
  2917.      case PF_8R8G8B:
  2918.         ctx->Driver.WriteColorSpan       = write_span_8R8G8B_ximage;
  2919.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_8R8G8B_ximage;
  2920.         ctx->Driver.WriteColorPixels     = write_pixels_8R8G8B_ximage;
  2921.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_8R8G8B_ximage;
  2922.         break;
  2923.      case PF_5R6G5B:
  2924.         ctx->Driver.WriteColorSpan       = write_span_5R6G5B_ximage;
  2925.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  2926.         ctx->Driver.WriteColorPixels     = write_pixels_5R6G5B_ximage;
  2927.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  2928.         break;
  2929.      case PF_DITHER:
  2930.         if (xmesa->xm_visual->visinfo->depth==8) {
  2931.            ctx->Driver.WriteColorSpan       = write_span_DITHER8_ximage;
  2932.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_DITHER8_ximage;
  2933.            ctx->Driver.WriteColorPixels     = write_pixels_DITHER8_ximage;
  2934.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_DITHER8_ximage;
  2935.         }
  2936.         else {
  2937.            ctx->Driver.WriteColorSpan       = write_span_DITHER_ximage;
  2938.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_DITHER_ximage;
  2939.            ctx->Driver.WriteColorPixels     = write_pixels_DITHER_ximage;
  2940.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_DITHER_ximage;
  2941.         }
  2942.         break;
  2943.      case PF_1BIT:
  2944.         ctx->Driver.WriteColorSpan       = write_span_1BIT_ximage;
  2945.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_1BIT_ximage;
  2946.         ctx->Driver.WriteColorPixels     = write_pixels_1BIT_ximage;
  2947.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_1BIT_ximage;
  2948.         break;
  2949.          case PF_HPCR:
  2950.             ctx->Driver.WriteColorSpan       = write_span_HPCR_ximage;
  2951.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_HPCR_ximage;
  2952.             ctx->Driver.WriteColorPixels     = write_pixels_HPCR_ximage;
  2953.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_HPCR_ximage;
  2954.             break;
  2955.          case PF_LOOKUP:
  2956.         if (xmesa->xm_visual->visinfo->depth==8) {
  2957.                ctx->Driver.WriteColorSpan       = write_span_LOOKUP8_ximage;
  2958.                ctx->Driver.WriteMonocolorSpan   = write_span_mono_LOOKUP8_ximage;
  2959.                ctx->Driver.WriteColorPixels     = write_pixels_LOOKUP8_ximage;
  2960.                ctx->Driver.WriteMonocolorPixels = write_pixels_mono_LOOKUP8_ximage;
  2961.             }
  2962.             else {
  2963.                ctx->Driver.WriteColorSpan       = write_span_LOOKUP_ximage;
  2964.                ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  2965.                ctx->Driver.WriteColorPixels     = write_pixels_LOOKUP_ximage;
  2966.                ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  2967.             }
  2968.             break;
  2969.          case PF_GRAYSCALE:
  2970.         if (xmesa->xm_visual->visinfo->depth==8) {
  2971.            ctx->Driver.WriteColorSpan       = write_span_GRAYSCALE8_ximage;
  2972.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_GRAYSCALE8_ximage;
  2973.            ctx->Driver.WriteColorPixels     = write_pixels_GRAYSCALE8_ximage;
  2974.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_GRAYSCALE8_ximage;
  2975.         }
  2976.         else {
  2977.            ctx->Driver.WriteColorSpan       = write_span_GRAYSCALE_ximage;
  2978.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  2979.            ctx->Driver.WriteColorPixels     = write_pixels_GRAYSCALE_ximage;
  2980.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  2981.         }
  2982.         break;
  2983.      default:
  2984.         die("Bad pixel format in xmesa_setup_DD_pointers (2)");
  2985.       }
  2986.    }
  2987.  
  2988.    /* Pixel/span reading functions: */
  2989.    ctx->Driver.ReadIndexSpan = read_index_span;
  2990.    ctx->Driver.ReadColorSpan = read_color_span;
  2991.    ctx->Driver.ReadIndexPixels = read_index_pixels;
  2992.    ctx->Driver.ReadColorPixels = read_color_pixels;
  2993. }
  2994.